Specifying versions when using nix declaratively

Hello all!

I want to use nix declaratively, as described in NixOS - Nixpkgs 21.11 manual. I have a single-user-installation on a ubuntu machine.

Here is something that I have written in my nixpkgs-config-file:

    myCollection = pkgs.buildEnv {
      name = "my-collection";
      paths = [
        go_1_18
        bat
      ];
      extraOutputsToInstall = [ "man" "doc" ];

Installing this package works quite well:

% nix-env --install --attr nixpkgs.myCollection
installing 'my-collection'
these derivations will be built:
  /nix/store/l10j8fgnzrj9ppmhs3xzfy2ql3jdf9fr-my-collection.drv
these paths will be fetched (2.32 MiB download, 5.82 MiB unpacked):
  /nix/store/8q43hvxghly0r3c7i2qd7069lh5axzk5-less-600
  /nix/store/az5qvil5l9vqbg0frni29727zacx4wbq-bat-0.21.0
copying path '/nix/store/8q43hvxghly0r3c7i2qd7069lh5axzk5-less-600' from 'https://cache.nixos.org'...
copying path '/nix/store/az5qvil5l9vqbg0frni29727zacx4wbq-bat-0.21.0' from 'https://cache.nixos.org'...
building '/nix/store/l10j8fgnzrj9ppmhs3xzfy2ql3jdf9fr-my-collection.drv'...
created 9 symlinks in user environment
building '/nix/store/gk1zvxrsxrb5pv77zwpn0br003l2pg3y-user-environment.drv'...
created 71 symlinks in user environment

However updating does not work well:

% nix-env --upgrade --always                                 
error: undefined variable 'go_1_18' at /home/user/.config/nixpkgs/config.nix:117:9
(use '--show-trace' to show detailed location information)
nix-env --upgrade --always  23.35s user 8.76s system 57% cpu 55.473 total

What is the reason for this behaviour? I want to comfortably update the self-created package. How can I do this?

1 Like

This is still relevant to me. Does someone have an answer? Or perhaps this is a clear bug and should be reported on Issues · NixOS/nix · GitHub?

1 Like

The manual example is bad. It uses a widely-scoped with statement which is considered an anti-pattern.

You need

paths = with pkgs; [
  go_1_18
  bat
];
1 Like

The bad example can be considered a bug. If you’re feeling up to it, you could fix the docs example with a PR.

1 Like

Hi @Atemu and thank you for your reply.

Adding with pkgs; seems to be not the only issue I have. Could you help me again? Probably it is something small. I tried it out with the following file:

{
  allowUnfree = true;
  packageOverrides = pkgs: with pkgs; {
    myCollection = pkgs.buildEnv {
      name = "my-collection";
      paths = with pkgs; [
        go_1_18
        bat
      ];
      extraOutputsToInstall = [ "man" "doc" ];
    };
  };
}

Then the following failing sequence happened:

% nix-env --install --attr nixpkgs.myCollection
installing 'my-collection'
these derivations will be built:
  /nix/store/nqi401122xck6g4s40waxrm5f0gf682r-my-collection.drv
these paths will be fetched (2.33 MiB download, 5.87 MiB unpacked):
  /nix/store/1wgqhayxkifd1aiq3c4lqwinbplrrxm0-less-600
  /nix/store/s86dg0d7pp4wrxx34bmxfkf2xayv21jb-bat-0.21.0
copying path '/nix/store/1wgqhayxkifd1aiq3c4lqwinbplrrxm0-less-600' from 'https://cache.nixos.org'...
# […]
created 71 symlinks in user environment
% nix-env --upgrade --always           
error: undefined variable 'go_1_18' at /home/user/.config/nixpkgs/config.nix:7:9
(use '--show-trace' to show detailed location information)

What nixpkgs revision/channel is used to upgrade the env? Go 1.18 doesn’t exist in 21.11 or earlier.

Hi Atemu,

I think you have found the issue. I installed “go_1_18” because just mentioning “go” as a package did not work and installed me go-1.16 in the past. Here is the reason:

~ % nix-channel --list
nixos https://nixos.org/channels/nixos-21.05
nixpkgs https://nixos.org/channels/nixpkgs-unstable

With removing the nixos-channel everything works as intended. Thank you!!! :slightly_smiling_face:

Looks like I smelled the right problem :wink:

Also make sure you update your channels regularly, else you won’t receive updates.

You can mark replies as solutions FYI

1 Like