NixOS Home Manager for multi-user on NIX flake Installation and Configuration

Ion Mudreac
4 min readSep 9, 2021

--

In the 1st article, we covered in detail NixOs native flake deployment

Once you have your system up and running in a purely declarative way next step, it to manage your personal configuration files located in $HOME /home folder. NixOs do not have any native implementation to manage user’s $HOME configuration folders and files. Few projects stepped in to help and manage users specific environments, and the most successful one in Home Manager

From the Home Manager site;
“This project provides a basic system for managing a user environment using the Nix package manager together with the Nix libraries found in Nixpkgs. It allows declarative configuration of user specific (non global) packages and dotfiles.”

A god start is Home Manager Manual
This article will cover Home Manager deployment in NixOS, With an example of configuration like configuring fish and bash shells, configuring tmux, neovim, and git with NIX native declarative configuration managed by Home Manager.
A good practice, we will store our Home Manager configuration files in the git repository similarly to what we did for the NixOs configuration.

Installation

Installation is very straightforward.

  1. Add home manager nix channel to users channel.

Note: make sure you are logged in as normal users and not as a root.
Note: Pay attention to the release version that should be the same as your NixOs version. In the below example, we use the latest NixOs and Home Manager release.

nix-channel --add https://github.com/nix-community/home-manager/archive/release-21.05.tar.gz home-manager

2. Update newly added channel

nix-channel --update

3. Home Manager installation

Note: Before installation is a good idea to logoff and login again before continuing with installation to avoid any potential issues with the missing path to the newly updated channel.

nix-shell '<home-manager>' -A install

Post Installation

Once home-manager and dependencies are installed, create a folder and file ~/.config/nixpkgs/home.nix. This file we will need to delete and replaced with pre-configured Home Manager from GitHub.

rm -rf ~/.config/nixpkgs/home.nixgit clone https://github.com/mudrii/hmtst.git ~/.config/nixpkgs --depth 1

Post GitHub repository sync-up good practice it to update flake.lock file to have the latest commits

nix flake update ~/.config/nixpkgs

Home Manager $USER environment deployment

Install all dependencies for the $USER.

home-manager switch --flake ~/.config/nixpkgs/#$USER -v

Deep Dive into Home Manager users configuration

flake.nix

Home Manager has full support for nix flake below if the flake I use to test and deploy my configuration.

{
description = "Home Manager NixOS configuration";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-21.05";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager.url = "github:nix-community/home-manager/release-21.05";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = inputs@{ self, nixpkgs, nixpkgs-unstable, home-manager, ... }:
{
homeConfigurations = {
mudrii = inputs.home-manager.lib.homeManagerConfiguration {
system = "x86_64-linux";
homeDirectory = "/home/mudrii";
username = "mudrii";
stateVersion = "21.05";
configuration = { config, pkgs, ... }:
let
overlay-unstable = final: prev: {
unstable = inputs.nixpkgs-unstable.legacyPackages.x86_64-linux;
};
in
{
nixpkgs.overlays = [ overlay-unstable ];
nixpkgs.config = {
allowUnfree = true;
allowBroken = true;
};
imports = [
./users/mudrii/home.nix
];
};
};
};
mudrii = self.homeConfigurations.mudrii.activationPackage;
defaultPackage.x86_64-linux = self.mudrii;
};
}

Note: First Thing to note, we added to inputs home manager remote Github repo.
Note: In section homeConfigurations you will need to replace with your $USER name.
Note: In addition, I added overlay-unstable to be able to install packages from the unstable channel/Github repo.

home.nix

Imports

imports = [
./dotfiles/bashrc.nix
./dotfiles/fish.nix
./dotfiles/git.nix
./dotfiles/tmux.nix
./dotfiles/neovim.nix
];

Note: home.nix allows us to import multiple configuration files and not to clutter everything in a single file.
Note: Check ./dotfiles/git.nix and update with your details the below fields.
Note: Fish shell is configured to use Oh My Fish one of the templates that I find it simple and useful.
Note: neovim is configured with few vim plugins in the stable and unstable branch that you may find it useful.

programs.git.userName = "Your Name here";
programs.git.userEmail = "your.email@domain.dom";

Note: you can change any configuration files that work best for you.

Packages

In home.nix, we provide a list of the packages that will be visible only for the user.

home = {
packages = with pkgs; [
sshfs
asciinema
aspell
aspellDicts.en
tldr
procs
gitAndTools.gh
git-crypt
git-lfs
gtop
bpytop
tree
ripgrep
file
binutils
fd
trash-cli
mosh
highlight
nix-index
yarn
nixpkgs-fmt
nixpkgs-review
pypi2nix
nodePackages.node2nix
unstable.python39Packages.poetry
(python39.withPackages (ps: with ps; [
pip
powerline
pygments
pynvim
]))
];
};

Programs

Note: For Some packages, Home Manager allows a different way to declare in home-manager style.

programs = {
home-manager.enable = true;
gpg.enable = true;
fzf.enable = true;
jq.enable = true;
bat.enable = true;
command-not-found.enable = true;
dircolors.enable = true;
htop.enable = true;
info.enable = true;
exa.enable = true;
direnv = {
enable = true;
nix-direnv = {
enable = true;
enableFlakes = true;
};
};

Services

In this section, we declare the services that will be available only for this user.

services = {
lorri.enable = true;
gpg-agent = {
enable = true;
enableSshSupport = true;
};
};

Fin

You can find tons of options to configure Home Manager in HM Manual
Additional information can be found in NixOs Wiki HM Page

--

--

Ion Mudreac
Ion Mudreac

Written by Ion Mudreac

Bank CTO at Advance Intelligence Group

No responses yet