owned this note
owned this note
Published
Linked with GitHub
---
tags: Publication
---
# NixOS Home Manager on native NIX flake installation and Configuration
In the 1st article, we covered in detail [NixOs native flake deployment](https://mudrii.medium.com/nixos-native-flake-deployment-with-luks-drive-encryption-and-lvm-b7f3738b71ca)
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](https://github.com/nix-community/home-manager)
From the Home Manager site;
"This project provides a basic system for managing a user environment using the [Nix](https://nixos.org/nix/) package manager together with the Nix libraries found in [Nixpkgs](https://nixos.org/nixpkgs/). It allows declarative configuration of user specific (non global) packages and dotfiles."
A god start is [Home Manager Manual](https://nix-community.github.io/home-manager/)
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._
```sh
nix-channel --add https://github.com/nix-community/home-manager/archive/release-21.05.tar.gz home-manager
```
2. Update newly added channel
```sh
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._
```sh
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.
```sh
rm -rf ~/.config/nixpkgs/home.nix
git clone https://github.com/mudrii/hmtst.git ~/.config/nixpkgs --depth 1
```
Post GitHub repository syncup good practice it to update flake.lock file to have the latest commits
```sh
nix flake update ~/.config/nixpkgs
```
## Home Manager `$USER` environment deployment
Install all dependencies for the `$USER`.
```sh
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.
```nix
{
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
```nix
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](https://github.com/oh-my-fish/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._
```nix
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.
```nix
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._
```nix
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.
```nix
services = {
lorri.enable = true;
gpg-agent = {
enable = true;
enableSshSupport = true;
};
};
```
## Fin
You can find tons of options to configure Home Manager in [HM Manual](https://nix-community.github.io/home-manager/options.html)
Additional information can be found in [NixOs Wiki HM Page](https://nixos.wiki/wiki/Home_Manager)