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

nixpkgs.config.allowUnfreePredicate does not work with flakes #118115

Closed
ShamrockLee opened this issue Mar 30, 2021 · 20 comments
Closed

nixpkgs.config.allowUnfreePredicate does not work with flakes #118115

ShamrockLee opened this issue Mar 30, 2021 · 20 comments

Comments

@ShamrockLee
Copy link
Contributor

Describe the bug

When using flake.nix along side configuration.nix to do nixos-rebuid, nixpkgs.config.allowUnfreePredicate can no longer be used to allow unfree package to build.

To Reproduce
Steps to reproduce the behavior:

  1. Enable flakes and use it to manage system configuration.
  2. Set nixpkgs.config.allowUnfreePredicate = (pkg: true)
  3. Set environment.systemPackages = [ hello-unfree ]; or use unfree options such as the nvidia driver.
  4. See the refuse-to-evaluate error.
    error: Package ‘example-unfree-package-1.0’ in /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/pkgs/applications/misc/hello-unfree/default.nix:19 has an unfree license (‘unfree’), refusing to evaluate.
    

Expected behavior
allowUnfreePredicate or equivalent options be available in configuration.nix or flake.nix

Screenshots
Screenshot_20210331_073234

Additional context

Notify maintainers

Metadata

  • system: "x86_64-linux"
  • host os: Linux 5.4.55, NixOS, 20.09.20200805.8e2b14a (Nightingale)
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.4pre20210308_1c0e3e4
  • channels(shamrock): "home-manager"
  • nixpkgs: /nix/var/nix/profiles/per-user/root/channels/nixos

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:
@FRidh
Copy link
Member

FRidh commented Mar 31, 2021

Flakes are evaluated hermetically . In a flake you need to explicitly import nixpkgs and pass in config with the options you want.

The error messages should be updated to document how one should do this with flakes.

@FRidh FRidh added this to the 21.05 milestone Mar 31, 2021
@vikanezrimaya
Copy link
Member

I am using flakes with allowUnfreePredicate right now and I cannot reproduce the issue. Can you provide the flake source code?

@vikanezrimaya
Copy link
Member

@FRidh NixOS module system already imports nixpkgs automatically and passes config to it. If the allowUnfreePredicate is set properly where it should be, there shouldn't be any problems. I don't think a documentation update is needed here since this option's semantics or usage pattern isn't changed or affected in any way by flakes.

@vikanezrimaya
Copy link
Member

vikanezrimaya commented Mar 31, 2021

It might also be that the hello-unfree package doesn't come from the same nixpkgs instance that NixOS uses, in which case the error will be produced. While not an error, it might make closures larger if different Nixpkgs revisions are used. In this case, the culprit nixpkgs instance needs to be imported and configured manually. If you're doing it inside configuration.nix, I suggest the following snippet:

(assuming you're using _module.args to inject different flakes, if not, modify this example):

{ config, pkgs, lib, olderNixpkgs }: let
  olderPkgs = import olderNixpkgs config.nixpkgs;
in {
  environment.systemPackages = with olderPkgs; [ hello-unfree ];
}

This will make two Nixpkgs configs identical.

@FRidh
Copy link
Member

FRidh commented Mar 31, 2021

@FRidh NixOS module system already imports nixpkgs automatically and passes config to it. If the allowUnfreePredicate is set properly where it should be, there shouldn't be any problems. I don't think a documentation update is needed here since this option's semantics or usage pattern isn't changed or affected in any way by flakes.

Right, I was mixing up nixos-rebuild with the nix commands.

@ShamrockLee
Copy link
Contributor Author

@kisik21 It can be reproduced with the following files:

/etc/nixos/flake.nix

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/8e2b14aceb1d40c7e8b84c03a7c78955359872bb";
  inputs.nixpkgs-nixos-20-09.url = "github:NixOS/nixpkgs/nixos-20.09";
  # inputs.nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = inputs@{self, nixpkgs, nixpkgs-nixos-20-09, ...}: let
      system = "x86_64-linux";
    in {
    nixosConfigurations.nixos-202007 = nixpkgs.lib.nixosSystem {
      inherit system;
      modules = [ ./configuration.nix ./hardware-configuration.nix
        "${nixpkgs-nixos-20-09}/nixos/modules/installer/tools/tools.nix"
      ];
      pkgs = import nixpkgs { inherit system; overlays = [
        (self: super:
        let
          # pkgs-nixos-20-09 = import nixpkgs-release-20-09 { inherit system; };
          pkgs-nixos-20-09 = (builtins.getAttr system nixpkgs-nixos-20-09.legacyPackages);
          # pkgs-unstable = import nixpkgs-unstable { inherit system; };
        in {
          # inherit (pkgs-nixos-20-09) openssl openssh;
          inherit (pkgs-nixos-20-09) sudo;
          inherit (pkgs-nixos-20-09) chromium chromiumBeta chromiumDev ungoogled-chromium;
          inherit (pkgs-nixos-20-09) firefoxPackages;
          inherit (pkgs-nixos-20-09) nix nixStable nixUnstable nixFlakes;
        })
      ];};
      extraArgs = {
        inherit nixpkgs-nixos-20-09;
      };
    };
  };
}

/etc/configuration.nix

{ config, pkgs, system, ... }:

{
  disabledModules = [ "installer/tools/tools.nix" ];

  boot.loader.efi.canTouchEfiVariables = true;

  # I hope to have Grub installed
  # boot.loader.grub.device = "/dev/disk/by-uuid/04CB-DC08";
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "nodev";
  boot.loader.grub.version = 2;
  boot.loader.grub.efiSupport = true;
  boot.loader.grub.useOSProber = true;

  nixpkgs.config.allowUnfreePredicate = (pkg: true);

  environment.systemPackages = with pkgs; [ hello-unfree ];
}

/etc/nixos/hardware-configuration.nix (Generated using nixos-generate-config)

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];

  boot.initrd.availableKernelModules = [ "xhci_pci" "ahci" "sd_mod" "sr_mod" "rtsx_pci_sdmmc" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/95673e06-72a1-428f-9c54-377ca2c7cf46";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/04CB-DC08";
      fsType = "vfat";
    };

  fileSystems."/home/shamrock/Documents" =
    { device = "/dev/disk/by-uuid/f702acd1-5cc6-42be-ba39-4058afb453fb";
      fsType = "ext4";
    };

  fileSystems."/home/shamrock/Downloads" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Downloads";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/home/shamrock/Music" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Music";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/home/shamrock/Pictures" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Pictures";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/home/shamrock/Videos" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Videos";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/var/lib/anbox/rootfs" =
    { device = "/nix/store/qhfx6fp5qsvlnkqf76n2awi8b6ajsh0r-android_amd64.img";
      fsType = "squashfs";
      options = [ "loop" ];
    };

  fileSystems."/var/lib/anbox/combined-rootfs" =
    { device = "overlay";
      fsType = "overlay";
    };

  fileSystems."/var/lib/anbox/combined-rootfs/cache" =
    { device = "/var/lib/anbox/cache";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/var/lib/anbox/combined-rootfs/data" =
    { device = "/var/lib/anbox/data";
      fsType = "none";
      options = [ "bind" ];
    };

  swapDevices =
    [ { device = "/dev/disk/by-uuid/115e7d69-e91b-4f8f-87f9-7cb5b722433e"; }
    ];

  powerManagement.cpuFreqGovernor = lib.mkDefault "powersave";
}

Here is the flake.lock generated by nixos-rebuild build

{
  "nodes": {
    "nixpkgs": {
      "locked": {
        "lastModified": 1596615455,
        "narHash": "sha256-qOp3QRL7dUbN+Kus7tReThC5ZuIqG9fy7/nkqlO78n8=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "8e2b14aceb1d40c7e8b84c03a7c78955359872bb",
        "type": "github"
      },
      "original": {
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "8e2b14aceb1d40c7e8b84c03a7c78955359872bb",
        "type": "github"
      }
    },
    "nixpkgs-nixos-20-09": {
      "locked": {
        "lastModified": 1617136447,
        "narHash": "sha256-JYvmMBbdu+LnlyWtcBjRqJJ9fOwd5bMcYaJW3M6bVy0=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "7640739f71e7afd18ad771255fa225d7753ab636",
        "type": "github"
      },
      "original": {
        "owner": "NixOS",
        "ref": "nixos-20.09",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "nixpkgs": "nixpkgs",
        "nixpkgs-nixos-20-09": "nixpkgs-nixos-20-09"
      }
    }
  },
  "root": "root",
  "version": 7
}

@vikanezrimaya
Copy link
Member

vikanezrimaya commented Mar 31, 2021

Oh, I see. You're setting pkgs in a call to nixosSystem, which overrides the config. Solution: use modules inside NixOS to actually configure Nixpkgs. Remove the pkgs argument in the call to nixosSystem and use the following in your configuration.nix:

{ config, pkgs, lib, nixpkgs-nixos-20-09, ... }: let
  stable-overlay = (self: super: let
    pkgs-nixos-20-09 = import nixpkgs-nixos-20-09 config.nixpkgs;
  in {
    # inherit (pkgs-nixos-20-09) openssl openssh;
    inherit (pkgs-nixos-20-09) sudo;
    inherit (pkgs-nixos-20-09) chromium chromiumBeta chromiumDev ungoogled-chromium;
    inherit (pkgs-nixos-20-09) firefoxPackages;
    inherit (pkgs-nixos-20-09) nix nixStable nixUnstable nixFlakes;
  });
in {
  nixpkgs.overlays = [ stable-overlay ];
}

Please report if this fixes your problem (it should, but I haven't tested if this fully evaluates properly!)

@ShamrockLee
Copy link
Contributor Author

@kisik21 The build failed, saying that it cannot find "system" or something.

It now says that the error is cached, and I don't know how to show it.

I'll write a toy configuration (as shown previously) later to reproduce this.

@Ma27
Copy link
Member

Ma27 commented Apr 3, 2021

@ShamrockLee you should be able to work around the cached failure thing by deleting ~/.cache/nix/eval-cache-v2.

IIRC there's no flag to force a re-evaluation although it seems possible on a low-level in the code. Since I got bitten by this in the past, I guess I'll file a patch for this against Nix :)

Ma27 added a commit to Ma27/nix that referenced this issue Apr 3, 2021
I think that it's not very helpful to get "cached failures" in a wrong
`flake.nix`. This can be very confusing when debugging a Nix expression.
See for instance NixOS/nixpkgs#118115.

In fact, the eval cache allows a forced reevaluation which is used for
e.g. `nix eval`. To provide a way around this for `nix build`, I added a
flag `--force-reevaluation` (and `-F` as shorthand) to repeatedly get
the error:

    λ ma27 [~/Projects/exp] → ../nix/outputs/out/bin/nix build -L --rebuild --experimental-features 'nix-command flakes'
    error: cached failure of attribute 'defaultPackage.x86_64-linux'

    λ ma27 [~/Projects/exp] → ../nix/outputs/out/bin/nix build -L --rebuild --experimental-features 'nix-command flakes' -F
    error: attribute 'hell' missing

           at /nix/store/mrnvi9ss8zn5wj6gpn4bcd68vbh42mfh-source/flake.nix:6:35:

                5|
                6|     packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hell;
                 |                                   ^
                7|
    (use '--show-trace' to show detailed location information)
@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Apr 5, 2021

@kisik21 The build failed, saying that it cannot find "system" or something.

It now says that the error is cached, and I don't know how to show it.

I'll write a toy configuration (as shown previously) later to reproduce this.

/etc/nixos/flake.nix

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/8e2b14aceb1d40c7e8b84c03a7c78955359872bb";
  inputs.nixpkgs-nixos-20-09.url = "github:NixOS/nixpkgs/nixos-20.09";

  outputs = inputs@{self, nixpkgs, nixpkgs-nixos-20-09, ...}: let
      system = "x86_64-linux";
    in {
    nixosConfigurations.nixos-202007 = nixpkgs.lib.nixosSystem {
      inherit system;
      modules = [ ./configuration.nix ./hardware-configuration.nix
        "${nixpkgs-nixos-20-09}/nixos/modules/installer/tools/tools.nix"
      ];
      extraArgs = {
        inherit nixpkgs-nixos-20-09;
      };
    };
  };
}

/etc/nixos/configuration.nix

{ config, pkgs, lib, system, nixpkgs-nixos-20-09, ... }:

let
  pkgs-nixos-20-09 = import nixpkgs-nixos-20-09 config.nixpkgs;
  stable-overlay = (self: super:
  let
  in {
    inherit (pkgs-nixos-20-09) openssh;
    inherit (pkgs-nixos-20-09) sudo;
    inherit (pkgs-nixos-20-09) nix nixStable nixUnstable nixFlakes;
    inherit (pkgs-nixos-20-09) chromium chromiumBeta chromiumDev ungoogled-chromium;
    inherit (pkgs-nixos-20-09) firefoxPackages;
    inherit (pkgs-nixos-20-09) links elinks lynx w3m;
    inherit (pkgs-nixos-20-09) git gitAndTools;
  });
in {
  disabledModules = [ "installer/tools/tools.nix" ];

  nixpkgs.config.overlays = [
    stable-overlay
  ];

  nixpkgs.config.allowUnfreePredicate = let
    getPnameFromPackage = pkg:
    pkg.pname or (builtins.parseDrvName pkg.name).name;
  in (pkg: let
      pname = getPnameFromPackage pkg;
    in
    builtins.trace "unfree package ${pkg.name or pkg.outPath or ""}, got pname=${pname}"
    (builtins.any (prefix: pkgs-nixos-20-09.lib.hasPrefix prefix pname) [
      "hello"
    ])
  );

  boot.loader.efi.canTouchEfiVariables = true;
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "nodev";
  boot.loader.grub.version = 2;
  boot.loader.grub.efiSupport = true;
  boot.loader.grub.useOSProber = true;

  networking.hostName = "nixos-202007";

  nix = {
    package = pkgs-nixos-20-09.nixFlakes;
    extraOptions = ''
      experimental-features = nix-command flakes
    '';
  };

  environment.systemPackages = with pkgs-nixos-20-09; [
    hello-unfree
  ];

}

/etc/nixos/hardware-configuration.nix

File content

/etc/nixos/hardware-configuration.nix

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];

  boot.initrd.availableKernelModules = [ "xhci_pci" "ahci" "sd_mod" "sr_mod" "rtsx_pci_sdmmc" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/95673e06-72a1-428f-9c54-377ca2c7cf46";
      fsType = "ext4";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/04CB-DC08";
      fsType = "vfat";
    };

  fileSystems."/home/shamrock/Documents" =
    { device = "/dev/disk/by-uuid/f702acd1-5cc6-42be-ba39-4058afb453fb";
      fsType = "ext4";
    };

  fileSystems."/home/shamrock/Downloads" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Downloads";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/home/shamrock/Music" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Music";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/home/shamrock/Pictures" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Pictures";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/home/shamrock/Videos" =
    { device = "/home/shamrock/Documents/shamrock-shared/bindings/Videos";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/var/lib/anbox/rootfs" =
    { device = "/nix/store/qhfx6fp5qsvlnkqf76n2awi8b6ajsh0r-android_amd64.img";
      fsType = "squashfs";
      options = [ "loop" ];
    };

  fileSystems."/var/lib/anbox/combined-rootfs" =
    { device = "overlay";
      fsType = "overlay";
    };

  fileSystems."/var/lib/anbox/combined-rootfs/cache" =
    { device = "/var/lib/anbox/cache";
      fsType = "none";
      options = [ "bind" ];
    };

  fileSystems."/var/lib/anbox/combined-rootfs/data" =
    { device = "/var/lib/anbox/data";
      fsType = "none";
      options = [ "bind" ];
    };

  swapDevices =
    [ { device = "/dev/disk/by-uuid/115e7d69-e91b-4f8f-87f9-7cb5b722433e"; }
    ];

  powerManagement.cpuFreqGovernor = lib.mkDefault "powersave";
}

/etc/nixos/flake.lock

File content
{
  "nodes": {
    "nixpkgs": {
      "locked": {
        "lastModified": 1596615455,
        "narHash": "sha256-qOp3QRL7dUbN+Kus7tReThC5ZuIqG9fy7/nkqlO78n8=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "8e2b14aceb1d40c7e8b84c03a7c78955359872bb",
        "type": "github"
      },
      "original": {
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "8e2b14aceb1d40c7e8b84c03a7c78955359872bb",
        "type": "github"
      }
    },
    "nixpkgs-nixos-20-09": {
      "locked": {
        "lastModified": 1617542311,
        "narHash": "sha256-YbYqpUA8EChwUaUSj8wIukv9ieFQdUnSSm5RypghNBA=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "91b77fe6942fe999b1efbe906dc98024d1917c0d",
        "type": "github"
      },
      "original": {
        "owner": "NixOS",
        "ref": "nixos-20.09",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "nixpkgs": "nixpkgs",
        "nixpkgs-nixos-20-09": "nixpkgs-nixos-20-09"
      }
    }
  },
  "root": "root",
  "version": 7
}

Output:

Terminal output
[shamrock@nixos-202007:/etc/nixos]$ sudo nixos-rebuild build --show-trace 2>&1 | tee nixos-rebuild_build_202104052159.txt  
tee: nixos-rebuild_build_202104052159.txt: Permission denied
warning: Git tree '/etc/nixos' is dirty
building the system configuration...
warning: Git tree '/etc/nixos' is dirty
error: assertion '(((args) ? localSystem) -> (! (((args) ? system) || ((args) ? platform))))' failed

       at /nix/store/snsfv9nmawna59cnqdghb2l72rrl2zac-source/pkgs/top-level/impure.nix:82:1:

           81| # not be passed.
           82| assert args ? localSystem -> !(args ? system || args ? platform);
             | ^
           83|

       … while evaluating anonymous lambda

       at /nix/store/snsfv9nmawna59cnqdghb2l72rrl2zac-source/pkgs/top-level/impure.nix:15:1:

           14|
           15| { # We combine legacy `system` and `platform` into `localSystem`, if
             | ^
           16|   # `localSystem` was not passed. Strictly speaking, this is pure desugar, but

       … from call site

       at /nix/store/h4n8621dxkd30im5j6r6c9mjxk8zgql8-source/configuration.nix:4:22:

            3| let
            4|   pkgs-nixos-20-09 = import nixpkgs-nixos-20-09 config.nixpkgs;
             |                      ^
            5|   stable-overlay = (self: super:

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:281:44:

          280|       defnsByName' = byName "config" (module: value:
          281|           [{ inherit (module) file; inherit value; }]
             |                                            ^
          282|         ) configs;

       … while evaluating 'dischargeProperties'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:467:25:

          466|   */
          467|   dischargeProperties = def:
             |                         ^
          468|     if def._type or "" == "merge" then

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:396:137:

          395|         defs' = concatMap (m:
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          397|         ) defs;

       … while evaluating definitions from `/nix/store/h4n8621dxkd30im5j6r6c9mjxk8zgql8-source/configuration.nix':

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:28:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                            ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:17:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                 ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:508:7:

          507|     in {
          508|       values = concatMap (def: if getPrio def == highestPrio then [(strip def)] else []) defs;
             |       ^
          509|       inherit highestPrio;

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:409:9:

          408|       in {
          409|         values = defs''';
             |         ^
          410|         inherit (defs'') highestPrio;

       … while evaluating the attribute 'mergedValue'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:415:5:

          414|     # Type-check the remaining definitions, and merge them. Or throw if no definitions.
          415|     mergedValue =
             |     ^
          416|       if isDefined then

       … while evaluating the option `nix.package':

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:383:9:

          382|     in opt //
          383|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          384|         inherit (res.defsFinal') highestPrio;

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:84:45:

           83|       yieldConfig = prefix: set:
           84|         let res = removeAttrs (mapAttrs (n: v:
             |                                             ^
           85|           if isOption v then v.value

       … from call site

       … while evaluating the attribute 'nix.package.out'

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:281:44:

          280|       defnsByName' = byName "config" (module: value:
          281|           [{ inherit (module) file; inherit value; }]
             |                                            ^
          282|         ) configs;

       … while evaluating 'dischargeProperties'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:467:25:

          466|   */
          467|   dischargeProperties = def:
             |                         ^
          468|     if def._type or "" == "merge" then

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:396:137:

          395|         defs' = concatMap (m:
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          397|         ) defs;

       … while evaluating definitions from `/nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/services/misc/nix-gc.nix':

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:28:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                            ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:17:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                 ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:508:7:

          507|     in {
          508|       values = concatMap (def: if getPrio def == highestPrio then [(strip def)] else []) defs;
             |       ^
          509|       inherit highestPrio;

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:409:9:

          408|       in {
          409|         values = defs''';
             |         ^
          410|         inherit (defs'') highestPrio;

       … while evaluating the attribute 'mergedValue'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:415:5:

          414|     # Type-check the remaining definitions, and merge them. Or throw if no definitions.
          415|     mergedValue =
             |     ^
          416|       if isDefined then

       … while evaluating the option `systemd.services.nix-gc.script':

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:383:9:

          382|     in opt //
          383|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          384|         inherit (res.defsFinal') highestPrio;

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:84:45:

           83|       yieldConfig = prefix: set:
           84|         let res = removeAttrs (mapAttrs (n: v:
             |                                             ^
           85|           if isOption v then v.value

       … from call site

       … while evaluating the attribute 'script'

       … while evaluating the attribute 'condition'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:552:14:

          551|     { _type = "if";
          552|       inherit condition content;
             |              ^
          553|     };

       … while evaluating the attribute 'condition'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:552:14:

          551|     { _type = "if";
          552|       inherit condition content;
             |              ^
          553|     };

       … while evaluating 'dischargeProperties'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:467:25:

          466|   */
          467|   dischargeProperties = def:
             |                         ^
          468|     if def._type or "" == "merge" then

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:396:137:

          395|         defs' = concatMap (m:
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          397|         ) defs;

       … while evaluating definitions from `/nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/system/boot/systemd.nix':

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:28:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                            ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:17:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                 ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:508:7:

          507|     in {
          508|       values = concatMap (def: if getPrio def == highestPrio then [(strip def)] else []) defs;
             |       ^
          509|       inherit highestPrio;

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:409:9:

          408|       in {
          409|         values = defs''';
             |         ^
          410|         inherit (defs'') highestPrio;

       … while evaluating the attribute 'mergedValue'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:415:5:

          414|     # Type-check the remaining definitions, and merge them. Or throw if no definitions.
          415|     mergedValue =
             |     ^
          416|       if isDefined then

       … while evaluating the option `systemd.services.nix-gc.serviceConfig':

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:383:9:

          382|     in opt //
          383|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          384|         inherit (res.defsFinal') highestPrio;

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:84:45:

           83|       yieldConfig = prefix: set:
           84|         let res = removeAttrs (mapAttrs (n: v:
             |                                             ^
           85|           if isOption v then v.value

       … from call site

       … while evaluating the attribute 'serviceConfig.Type'

       … while evaluating 'optional'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:254:20:

          253|   */
          254|   optional = cond: elem: if cond then [elem] else [];
             |                    ^
          255|

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/system/boot/systemd.nix:894:10:

          893|         restart = service.serviceConfig.Restart or "no";
          894|       in optional
             |          ^
          895|       (type == "oneshot" && (restart == "always" || restart == "on-success"))

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/system/boot/systemd.nix:890:51:

          889|
          890|     warnings = concatLists (mapAttrsToList (name: service:
             |                                                   ^
          891|       let

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/attrsets.nix:234:16:

          233|   mapAttrsToList = f: attrs:
          234|     map (name: f name attrs.${name}) (attrNames attrs);
             |                ^
          235|

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/attrsets.nix:234:10:

          233|   mapAttrsToList = f: attrs:
          234|     map (name: f name attrs.${name}) (attrNames attrs);
             |          ^
          235|

       … from call site

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:281:44:

          280|       defnsByName' = byName "config" (module: value:
          281|           [{ inherit (module) file; inherit value; }]
             |                                            ^
          282|         ) configs;

       … while evaluating 'dischargeProperties'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:467:25:

          466|   */
          467|   dischargeProperties = def:
             |                         ^
          468|     if def._type or "" == "merge" then

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:396:137:

          395|         defs' = concatMap (m:
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))
             |                                                                                                                                         ^
          397|         ) defs;

       … while evaluating definitions from `/nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/system/boot/systemd.nix':

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:28:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                            ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:395:17:

          394|         # Process mkMerge and mkIf properties.
          395|         defs' = concatMap (m:
             |                 ^
          396|           map (value: { inherit (m) file; inherit value; }) (builtins.addErrorContext "while evaluating definitions from `${m.file}':" (dischargeProperties m.value))

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:508:7:

          507|     in {
          508|       values = concatMap (def: if getPrio def == highestPrio then [(strip def)] else []) defs;
             |       ^
          509|       inherit highestPrio;

       … while evaluating the attribute 'values'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:409:9:

          408|       in {
          409|         values = defs''';
             |         ^
          410|         inherit (defs'') highestPrio;

       … while evaluating the attribute 'mergedValue'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:415:5:

          414|     # Type-check the remaining definitions, and merge them. Or throw if no definitions.
          415|     mergedValue =
             |     ^
          416|       if isDefined then

       … while evaluating the option `warnings':

       … while evaluating the attribute 'value'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:383:9:

          382|     in opt //
          383|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          384|         inherit (res.defsFinal') highestPrio;

       … while evaluating anonymous lambda

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/modules.nix:84:45:

           83|       yieldConfig = prefix: set:
           84|         let res = removeAttrs (mapAttrs (n: v:
             |                                             ^
           85|           if isOption v then v.value

       … from call site

       … while evaluating the attribute 'warnings'

       … while evaluating 'fold''

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:55:15:

           54|       len = length list;
           55|       fold' = n:
             |               ^
           56|         if n == len

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:59:8:

           58|         else op (elemAt list n) (fold' (n + 1));
           59|     in fold' 0;
             |        ^
           60|

       … while evaluating 'foldr'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:52:20:

           51|   */
           52|   foldr = op: nul: list:
             |                    ^
           53|     let

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/trivial.nix:302:33:

          301|
          302|   showWarnings = warnings: res: lib.fold (w: x: warn w x) res warnings;
             |                                 ^
          303|

       … while evaluating 'showWarnings'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/trivial.nix:302:28:

          301|
          302|   showWarnings = warnings: res: lib.fold (w: x: warn w x) res warnings;
             |                            ^
          303|

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/system/activation/top-level.nix:125:10:

          124|     then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
          125|     else showWarnings config.warnings baseSystem;
             |          ^
          126|

       … while evaluating 'fold''

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:55:15:

           54|       len = length list;
           55|       fold' = n:
             |               ^
           56|         if n == len

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:59:8:

           58|         else op (elemAt list n) (fold' (n + 1));
           59|     in fold' 0;
             |        ^
           60|

       … while evaluating 'foldr'

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/lib/lists.nix:52:20:

           51|   */
           52|   foldr = op: nul: list:
             |                    ^
           53|     let

       … from call site

       at /nix/store/17457fc7h1d2z9ivm458xvka6nq6lv0n-source/nixos/modules/system/activation/top-level.nix:128:12:

          127|   # Replace runtime dependencies
          128|   system = fold ({ oldDependency, newDependency }: drv:
             |            ^
          129|       pkgs.replaceDependency { inherit oldDependency newDependency drv; }

@ShamrockLee
Copy link
Contributor Author

Also, if the lines about pkgs-nixos-20-09 is imported as

{ config, pkgs , system, lib, ... }: let
  pkgs-nixos-20-09 = import nixpkgs-20-09 { inherit system; };
  # ...
in {
  # ...
}

nixos-rebuild will complain

error: attribute 'system' missing

Ma27 added a commit to Ma27/nix that referenced this issue Apr 19, 2021
I think that it's not very helpful to get "cached failures" in a wrong
`flake.nix`. This can be very confusing when debugging a Nix expression.
See for instance NixOS/nixpkgs#118115.

In fact, the eval cache allows a forced reevaluation which is used for
e.g. `nix eval`.

This change makes sure that this is the case for `nix build` as well. So
rather than

    λ ma27 [~/Projects/exp] → ../nix/outputs/out/bin/nix build -L --rebuild --experimental-features 'nix-command flakes'
    error: cached failure of attribute 'defaultPackage.x86_64-linux'

the evaluation of already-evaluated (and failed) attributes looks like
this now:

    λ ma27 [~/Projects/exp] → ../nix/outputs/out/bin/nix build -L --rebuild --experimental-features 'nix-command flakes'
    error: attribute 'hell' missing

           at /nix/store/mrnvi9ss8zn5wj6gpn4bcd68vbh42mfh-source/flake.nix:6:35:

                5|
                6|     packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hell;
                 |                                   ^
                7|
    (use '--show-trace' to show detailed location information)
@stale
Copy link

stale bot commented Oct 22, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 22, 2021
@nixos-discourse
Copy link

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nixos-config-in-flakes-crash-on-impure-nix/16178/1

@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Nov 20, 2021
@Artturin Artturin modified the milestones: 21.05, 23.05 Dec 31, 2022
@ncfavier
Copy link
Member

ncfavier commented Jan 5, 2023

error: attribute 'system' missing

Your module takes a system argument, but nothing sets that. You should use pkgs.system or config.nixpkgs.system instead.


pkgs-nixos-20-09 = import nixpkgs-nixos-20-09 config.nixpkgs;

You want

import nixpkgs-nixos-20-09 { inherit (config.nixpkgs) localSystem crossSystem config overlays; }

I think we might want to add a warning in https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/misc/nixpkgs.nix if both nixpkgs.pkgs and nixpkgs.config are set, to make it clear that the latter is ignored. cc @roberth

@roberth
Copy link
Member

roberth commented Jan 8, 2023

You should use pkgs.system or config.nixpkgs.system instead.

Not config.nixpkgs.system because it's also ignored when pkgs is set directly. Furthermore it is ill-defined in the context of cross compilation. pkgs.stdenv.hostPlatform.system will always give you the system for the final host.

pkgs.system should be equivalent, but I think someone wanted to deprecate that.

add a warning

sgtm

@ncfavier
Copy link
Member

ncfavier commented Jan 9, 2023

I'm second-guessing this now. What if something outside the user's control sets nixpkgs.config, but they want to set nixpkgs.pkgs anyway? That would just be an annoying warning.

@ShamrockLee
Copy link
Contributor Author

ShamrockLee commented Apr 22, 2024

It has been 3 years since I opened this issue. I suspect that the problem is caused by direct specification of the pkgs argument when calling lib.nixosSystem, for I cannot reproduce it without specifying the pkgs there. Thank you all for helping out!

Now I think pkgs-wide overriding should be done inside configuration.nix instead of the pkgs argument of lib.nixosSystem wherever possible.

The "cached failure" of flake when doing flake-based nixos-rebuild still bites, but that's out of the scope of this issue.

I think we might want to add a warning in https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/misc/nixpkgs.nix if both nixpkgs.pkgs and nixpkgs.config are set, to make it clear that the latter is ignored. cc @roberth

Should we close it now, or should we document such shadowing behavior? (Not sure where nixpkgs.lib.nixosSystem is documented.)

@eclairevoyant
Copy link
Member

There's an assertion added in ce87196 which should prevent this:

{
assertion = opt.pkgs.isDefined -> cfg.config == {};
message = ''
Your system configures nixpkgs with an externally created instance.
`nixpkgs.config` options should be passed when creating the instance instead.
Current value:
${lib.generators.toPretty { multiline = true; } opt.config}

If a user really wants to externally configure nixpkgs, they can add the module nixpkgs.nixosModules.readOnlyPkgs and set nixpkgs.pkgs

@Ma27
Copy link
Member

Ma27 commented Apr 22, 2024

The "cached failure" of flake when doing flake-based nixos-rebuild still bites, but that's out of the scope of this issue.

It bites again to be precise ;-)
There are pending fixes upstream though.

@roberth
Copy link
Member

roberth commented Apr 22, 2024

nixosSystem used to do too much. I'm proposing to eventually remove unneeded arguments

(Not sure where nixpkgs.lib.nixosSystem is documented.)

Unfortunately it was never documented.

We should run nixdoc on the flake lib (after some processing to remove the Nixpkgs lib from the flake lib). Something like this would be helpful as a preparation for that:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests