DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • AWS IAM Basics: Identity and Access Management
  • NixOS and Home Manager Update With Nix Systemd Services
  • Google Cloud “GCP” native NixOS images build
  • NixOs Native Flake Deployment With LUKS Drive Encryption and LVM

Trending

  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  • Event-Driven Pipelines With Apache Pulsar and Go
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes

NixOS Home Manager for Multi-User on NIX Flake Installation and Configuration

If you are interested in learning more about Nix and NixOS as pure functional configurations, language is a good start. Read on to find out more!

By 
Ion Mudreac user avatar
Ion Mudreac
·
Sep. 13, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.0K Views

Join the DZone community and get the full member experience.

Join For Free

In the first article, we covered in detail NixOs native flake deployment.

Once you have your system up and running in a purely declarative way, the next step is to manage your personal configuration files located in $HOME /home folder. NixOs does 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 is 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 good 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.

As 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


  1. Update newly added channel
nix-channel --update


  1. 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 preconfigured Home Manager from GitHub.

rm -rf ~/.config/nixpkgs/home.nix

git 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 the homeConfigurations section, you will need to replace it 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 the ./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 useful.

programs.git.userName = "Your Name here";
programs.git.userEmail = "[email protected]";


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

Packages

In the home.nix file, 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 the HM Manual.
Additional information can be found on the NixOs Wiki HM Page.

NixOS Multi-user

Opinions expressed by DZone contributors are their own.

Related

  • AWS IAM Basics: Identity and Access Management
  • NixOS and Home Manager Update With Nix Systemd Services
  • Google Cloud “GCP” native NixOS images build
  • NixOs Native Flake Deployment With LUKS Drive Encryption and LVM

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook