LnL7 / nix-darwin

nix modules for darwin
MIT License
3.19k stars 457 forks source link

Put nixpkgs in NIX_PATH and system registry for flake configs #1067

Closed antoineco closed 2 months ago

antoineco commented 2 months ago

Backport of NixOS/nixpkgs@e456032addae76701eb17e6c03fc515fd78ad74f, which ensures that NIX_PATH includes nixpkgs=flake:nixpkgs when the system was built from a flake.

Here is the rationale for having NIX_PATH correctly set in a flake context, especially since Nix 2.24: https://github.com/NixOS/nixpkgs/pull/273170#issuecomment-2342254448


💻 Demonstration from a REPL session, using my own system flake importing this branch

  1. Before switch, despite having extra-nix-path = nixpkgs=flake:nixpkgs in nix.conf:

    $ nix repl --expr 'import <nixpkgs> {}'
    Nix 2.24.6
          error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I)
  2. Open the REPL and load the flake's output to execute nix-darwin.lib.darwinSystem:

    $ nix repl
    Nix 2.24.6
    Type :? for help.
    >
    > darwinCfg = (builtins.getFlake (builtins.toString ./.)).darwinConfigurations.myhost
  3. Verify that nix.registry was populated:

    • With nix.registry.nixpkgs equal to nix-darwin's nixpkgs.source (default)

      > darwinCfg.options.nix.registry.value.nixpkgs.to
      {
      path = { ... };
      type = "path";
      }
      > builtins.toPath darwinCfg.options.nix.registry.value.nixpkgs.to.path
      "/nix/store/1h99qq6970gkx3j0m9w4yrrl9y99y1nk-source"
    • With nix.registry.nixpkgs overriden by the user

      > darwinCfg.options.nix.registry.value.nixpkgs.to
      {
      type = "tarball";
      url = "https://flakehub.com/f/DeterminateSystems/nixpkgs-weekly/0.1.0.tar.gz";
      }
  4. Verify that nix.nixPath was populated:

    • With nix.channel.enable = true (default)

      > darwinCfg.options.nix.nixPath.value
      [
      "nixpkgs=flake:nixpkgs"
      "/nix/var/nix/profiles/per-user/root/channels"
      ]
    • With nix.channel.enable = false

      > darwinCfg.options.nix.nixPath.value
      [ "nixpkgs=flake:nixpkgs" ]
  5. After a switch, the system can perform <nixpkgs> lookups successfully:

    $ darwin-rebuild switch
    $ exec zsh -il
    $ nix repl --expr 'import <nixpkgs> {}'
    Nix 2.24.6
    Type :? for help.
    Loading installable ''...
    Added 22627 variables.