Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

antoineco
Copy link
Contributor

@antoineco antoineco commented Sep 10, 2024

Backport of NixOS/nixpkgs@e456032, 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: NixOS/nixpkgs#273170 (comment)


💻 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.

antoineco added a commit to antoineco/dotfiles that referenced this pull request Sep 10, 2024
Reverts commit 0e50273
Reverts commit 2ff377b
Reverts commit fb132b9

nix-darwin now behaves like NixOS and mangles fpath inside
/etc/zshenv[1].

[1]: LnL7/nix-darwin#1067
Currently there are a bunch of really wacky hacks required to get
nixpkgs path correctly set up under flake configs such that `nix run
nixpkgs#hello` and `nix run -f '<nixpkgs>' hello` hit the nixpkgs that
the system was built with. In particular you have to use specialArgs or
an anonymous module, and everyone has to include this hack in their own
configs.

We can do this for users automatically.

NixOS/nixpkgs@e456032

Co-authored-by: Antoine Cotten <hello@acotten.com>
@antoineco antoineco changed the title flake: put nixpkgs in NIX_PATH and system registry for flake configs Put nixpkgs in NIX_PATH and system registry for flake configs Sep 11, 2024
Copy link
Collaborator

@emilazy emilazy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually somewhat dislike this change because it makes nix flake lock behave weirdly for flakes that take a bare nixpkgs input (and IIRC I made the lock file of either nix-darwin or Home Manager weird for a while precisely because of this), but we should be consistent with NixOS here, so LGTM.

@emilazy emilazy merged commit a0e362a into LnL7:master Sep 12, 2024
6 checks passed
@antoineco antoineco deleted the module/nixpkgs-flake branch September 12, 2024 10:07
antoineco added a commit to antoineco/dotfiles that referenced this pull request Sep 12, 2024
Reverts commit aaac9dc

The value of NIX_PATH in nix-darwin is now consistent with this of
NixOS[1] so Nix 2.24+ works as expected.

[1]: LnL7/nix-darwin#1067
bigolu added a commit to bigolu/system-configurations that referenced this pull request Sep 15, 2024
- nix-darwin now sets the flake registry to that of the nixpkgs passed to nix-darwin.lib.darwinSystem. Not only do I no longer need to do it, but I got an error when trying to rebuild my system because the option was being set in two place. LnL7/nix-darwin#1067
- apparently auto-allocate-uids is broken so I'm not going to enable it anymore: DeterminateSystems/nix-installer#580 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants