^
< previous study | top | next study >
1. Bio / Persona
10+ years of Linux/UNIX experience; uses NixOS, OpenBSD, and Windows as daily drivers. Proficient with C, Perl, and Common LISP. Uses Salt as deployment tool.
2. Prior Nix experience
Self-professed Nix beginner, NixOS user for 2-3 years. Already wrote new packages, and contributed to Nixpkgs.
Wants to learn
- the Nix language properly
- how to write NixOS modules and services
3. Using Nix
Goal for today’s session is to have some scripts available on the system.
Goes to /etc/nixos/configuration.nix
, adds a file.nix
to imports, then opens file.nix
, copy-pastes the header from configuration.nix
, and stops - no idea how to proceed.
-
First off, tries to configure video drivers.
Googles
nvidia nixos
, opens “Nvidia” article on the nixos.wiki. Starts reading page, and stops at first full configuration example after “A possible configuration:”. Copy-pasteswriteShellScriptBin
, replaces its contents, then copiesenvironment.systemPackages
. Issuesnixos-rebuild test --flake
, which works, so adds to the configuration git repo. -
Next, declare a
tmux
configuration file as/home/my-user/tmux.conf
.“Can I just describe a file and put it into
PATH
?” Googlesnixos declare file
.-
Discourse: Creating directories and files declaratively
Skims thread, not about what is needed for this.
-
NixOS/nixpkgs
(still open) issue: Declarative management of user environments and config files #1750Old comments state that NixOS by itself does not manage user environments, but most of the conversation is about trying to manage dotfiles with Nix on Debian, which does not apply.
Last comment refers to nix-cookbook FAQ, which redirects to an unmaintained part of nix.dev. The old page does not exist anymore, but nix.dev’s FAQ has an entry with the same title; the answer is simply a link to the Home Manager repo.
Already uses Home Manager for impermanence (explains it as a kind of “reproducible user environment”), but did not see the point using it with NixOS. After giving it another look, it appears that one can define user profiles too, but still looks like a dead end for what is needed right now.
-
Back to nix-dev FAQ, checks headings, but nothing relevant.
-
Returns to Home Manager, checks out the configuration options, but still can’t fathom how it could possibly help with dotfiles.
-
Googles “home-manager dotfile”
-
Opens Managing dotfiles with Nix blog post
Scans the text trying to find how to declare dotfiles, gets to the “Configuration” section, and sees a Home Manager configuration snippet:
This
home.nix
enables the bat Home Manager module and sets some configuration:{ config, pkgs, ... }: { home.username = "apearce"; # ... programs.bat = { enable = true; config = { theme = "GitHub"; italic-text = "always"; }; }; }
“Maybe I need to write a NixOS module to use something like this to configure
tmux
?” Looks up the custombat
module in the Home Manager repo, and ponders how to get started writing a module fortmux
when sees that that there is one already defined for it.“Maybe Home Manager is the right tool for what I want after all, but it’s not obvious how to define dotfiles.”
-
-
Interviewer:
Home Manager is exactly what you are looking for. It wasn’t clear from the documentation that it provides modules for many things.
Participant:
Oh, I thought it was similar tousers.users.<>.packages
.
Visits the Home Manager manual once more, and finds the highlighted line explaining what Home Manager is for (emphasis mine):
Chapter 1. Installing Home Manager
Home Manager can be used in three primary ways:
- Using the standalone home-manager tool. For platforms other than NixOS and Darwin, this is the only available choice. It is also recommended for people on NixOS or Darwin that want to manage their home directory independently of the system as a whole. See Section 1.1, “Standalone installation” for instructions on how to perform this installation.
It is well buried, and the manual assumes that people who get here already know what Home Manager is about and what problems it is trying to solve. (Namely, the one found above: Declarative management of user environments and config files #1750.)
Chapter 2. Using Home Manager gives a quick example, but it is confusing as “it says in 1.2. NixOS module that we’re not supposed to use the home-manager
command on NixOS, but the manual does exactly that in Chapter 2. The inconsistency makes examples confusing to read.”
Gives installing it another try.
- Goes directly to the NixOS and flake-specific instructions,
- copies the configuration example into
/etc/nixos/flake.nix
, - adds inputs.
Turns out that previous installation attempts failed, because import ./home.nix
in the example is ambiguous: expected Home Manager to take the relative directory to user home; that is, the absolute path would become /home/my-user/home.nix
, and not ~/.config/nixpkgs/home.nix
. Tried to use the full path too instead of ./home.nix
, but kept getting the following error:
$ nixos rebuild --test
error: access to absolute path '/home/my-user/home.nix' is forbidden in pure eval mode (use '--impure' to override)
(use '--show-trace' to show detailed location information)
Spent about 40 minutes to figure this out. Used the home-manager
command without root, based on the example. What would really help is seeing a complete configuration file after evaluation.
In the end, the task has been achieved the task, and learned a few things. The session ends here.