About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(BSD OpenBSD Qubes OS Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. OpenBSD developer solene@. No AI is involved in this blog.

Contact me: solene at dataswamp dot org or @solene@bsd.network (mastodon).

You can sponsor my work financially if you want to help me writing this blog and contributing to Free Software as my daily job.

Make nix flakes commands using the same nixpkgs as NixOS does

Written by Solène, on 20 July 2022.
Tags: #nixos #linux #nix

Comments on Fediverse/Mastodon

1. Introduction §

This article will explain how to make the flakes enabled nix commands reusing the nixpkgs repository used as input to build your NixOS system. This will regularly save you time and bandwidth.

2. Flakes and registries §

By default, nix commands using flakes such as nix shell or nix run are pulling a tarball of the development version of nixpkgs. This is the default value set in the nix registry for nixpkgs.

$ nix registry list | grep nixpkgs
global flake:nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable

Because of this, when you run a command, you are likely to download a tarball of the nixpkgs repository including the latest commit every time you use flakes, this is particularly annoying because the tarball is currently around 30 MB. There is a simple way to automatically set your registry to define the nixpkgs repository to the local archive used by your NixOS configuration.

To your flake.nix file describing your system configuration, you should have something similar to this:

inputs.nixpkgs.url = "nixpkgs/nixos-unstable";

[...]
nixosConfiguration = {
  my-computer =lib.nixosSystem {
    specialArgs = { inherit inputs; };
    [...]
  };
};

Edit /etc/nixos/configuration.nix and make sure you have "inputs" listed in the first line, such as:

{ lib, config, pkgs, inputs, ... }:

And add the following line to the file, and then rebuild your system.

nix.registry.nixpkgs.flake = inputs.nixpkgs;

After this change, running a command such as "nix shell nixpkgs#gnumake" will reuse the same nixpkgs from your nix store used by NixOS, otherwise it would have been fetching the latest archive from GitHub.

3. nix-shell vs nix shell §

If you started using flakes, you may wonder why there are commands named "nix-shell" and "nix shell", they work totally differently.

nix-shell and non flakes commands use the nixpkgs offered in the NIX_PATH environment variable, which should be set to a directory managed by nix-channel, but the channels are obsoleted by flakes...

Fortunately, in the same way we synchronized the system flakes with the commands flakes, you can add this code to use the system nixpkgs with your nix-shell:

nix.nixPath = [ "nixpkgs=/etc/channels/nixpkgs" "nixos-config=/etc/nixos/configuration.nix" "/nix/var/nix/profiles/per-user/root/channels" ];
environment.etc."channels/nixpkgs".source = inputs.nixpkgs.outPath;

This requires your user to logout from your current session to be effective. You can then check nix-shell and nix shell use the same nixpkgs source with this snippet. This asks the full path of the test program named "hello" and compares both results, they should match if they use the same nixpkgs.

[ "$(nix-shell -p hello --run "which hello")" = "$(nix shell nixpkgs#hello -c which hello)" ] && echo success

4. Conclusion §

Flakes are awesome, and are in the way of becoming the future of Nix. I hope this article shed some light about nix commands, and saved you some bandwidth.

5. Credits §

I found this information on a blog post of the company Tweag (which is my current employer) in a series of articles about Nix flakes. That's a bit sad I didn't find this information in the official NixOS documentation, but as flakes are still experimental, they are not really covered.

Tweag blog: Nix Flakes, Part 3: Managing NixOS systems

As I found this information on their blog post, and I'm fine giving credits to people, so I have to link their blog post license here.

Creative Commons Attribution 4.0 International license