Skip to content

Instantly share code, notes, and snippets.

@newtolinux23
Last active April 25, 2025 14:32
Show Gist options
  • Select an option

  • Save newtolinux23/f685deeb4d6d25e3a20db364eae1f799 to your computer and use it in GitHub Desktop.

Select an option

Save newtolinux23/f685deeb4d6d25e3a20db364eae1f799 to your computer and use it in GitHub Desktop.
This document outlines the steps I followed to install Home Manager on NixOS using flakes, detailing the configuration process and issues encountered along the way.

Installing and Using Home Manager on NixOS with Flakes

https://dz2cdn1.dzone.com/storage/temp/14989350-nixos-hires.png

Table of Contents

Introduction

This document outlines the steps I followed to install Home Manager on NixOS using flakes, detailing the configuration process, issues encountered, and how to use Home Manager.

Setup NixOS with Flakes

  1. Ensure that you have NixOS installed and that flakes are enabled. Add the following to your /etc/nixos/configuration.nix:
    nix = {
      package = pkgs.nixFlakes;
      extraOptions = ''
        experimental-features = nix-command flakes
      '';
    };
        
  2. Apply the configuration:
    sudo nixos-rebuild switch
        

Configure Home Manager

  1. Create a flake.nix file to configure NixOS with Home Manager:
    {
      description = "NixOS configuration for Rob's system";
    
      inputs = {
        nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
        home-manager.url = "github:nix-community/home-manager";
        home-manager.inputs.nixpkgs.follows = "nixpkgs";
      };
    
      outputs = { self, nixpkgs, home-manager }: {
        nixosConfigurations = {
          rob-nixos = nixpkgs.lib.nixosSystem {
            system = "x86_64-linux";
            modules = [
              ./configuration.nix
              home-manager.nixosModules.home-manager
              {
                home-manager.useGlobalPkgs = true;
                home-manager.useUserPackages = true;
                home-manager.backupFileExtension = "backup"; # Set backup file extension
                home-manager.users.rob = import ./home.nix {
                  pkgs = import nixpkgs {
                    system = "x86_64-linux";
                  };
                  lib = nixpkgs.lib;
                  config = {};
                };
              }
            ];
          };
        };
      };
    }
        
  2. Create a home.nix file for Home Manager configuration:
    { config, pkgs, lib, ... }:
    
    {
      home.username = "rob";
      home.homeDirectory = "/home/rob";
    
      programs.home-manager.enable = true;
    
      home.stateVersion = "23.05"; # Specify the version of Home Manager
    
      programs.bash = {
        enable = true;
        initExtra = ''
          # Add any custom initialization for bash here
          export EDITOR="nano"
        '';
      };
    
      home.packages = with pkgs; [
        pkgs.nano
        pkgs.vim
        pkgs.git
        pkgs.wget
      ];
    
      home.sessionVariables = {
        EDITOR = "nano";
      };
    }
        
  3. Apply the configuration with flakes:
    sudo nixos-rebuild switch --flake .#
        

Issues Encountered

Undefined Variable ‘system’

Initially, I encountered an error related to the undefined variable 'system':

error: undefined variable ‘system’

This was resolved by explicitly defining 'system' when importing nixpkgs within flake.nix.

Missing ‘stateVersion’

Another issue was the missing 'stateVersion', which is required for Home Manager:

error: The option `home-manager.users.rob.home.stateVersion’ is used but not defined.

This was fixed by adding the 'stateVersion' option in home.nix.

BackupFileExtension Configuration

There were conflicts with existing files, which required setting a backup file extension:

error: The option `home-manager.users.rob.home-manager’ does not exist.

This was resolved by correctly placing the 'backupFileExtension' option in flake.nix under the home-manager module.

Using Home Manager

Once Home Manager is installed and configured, you can start using it to manage your home environment.

Managing Packages

To manage packages, add them to the home.packages attribute in your home.nix file. For example:

home.packages = with pkgs; [
  pkgs.nano
  pkks.vim
  pkgs.git
  pkgs.wget
];

Setting Configuration Options

You can set various configuration options for your user environment in the home.nix file. For example, to set the default editor to nano:

home.sessionVariables = {
  EDITOR = "nano";
};

Applying Home Manager Configuration

After making changes to your home.nix file, apply the new configuration using the following command:

home-manager switch

Using Home Manager with GitHub

To manage your Home Manager configuration with GitHub, follow these steps:

  1. Initialize a Git repository in your configuration directory:
    git init
        
  2. Add your configuration files to the repository:
    git add flake.nix home.nix configuration.nix
        
  3. Commit the changes:
    git commit -m "Initial commit of Home Manager configuration"
        
  4. Create a repository on GitHub, and then add it as a remote in your local repository:
    git remote add origin https://github.com/yourusername/yourrepository.git
        
  5. Push the changes to GitHub:
    git push -u origin master
        

Now your Home Manager configuration is version-controlled and hosted on GitHub. You can pull changes from GitHub on other machines and apply the configuration using the following commands:

git pull origin master
sudo nixos-rebuild switch --flake .#
home-manager switch

Conclusion

By following these steps and resolving the issues encountered, I successfully installed and configured Home Manager on NixOS using flakes. This setup allows for efficient management of the home environment and seamless updates using the flake-based approach. Additionally, managing the configuration with GitHub ensures that changes are version-controlled and easily deployable across multiple machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment