Trouble using `nixpkgs.config.allowUnfree` with Flakes module system

Hello, I’m brand-new to Nix and NixOS. I like the concept, like the language, but hate the (some of the) syntax and the lack of documentation.

I am trying to split my config up into different modules in preparation for the future. One of the changes I’ve made is the use of Flakes.

The issue that I’m having now is that --flake is complaining about my configuration being impure. Why wasn’t the regular command that used configuration.nix directly flagging me down with similar complaints?

If I remove the following:

  nixpkgs.config.allowUnfree = true;
  nixpkgs.config.allowBroken = true;

From configuration.nix, it complains about something else. But I need to get past this first.

error: cannot look up '<nixos-config>' in pure evaluation mode (use '--impure' to override)

       at /nix/store/mrf1rcmvfwsgdcrcvgg06mfz12im6inf-source/nixos/modules/system/activation/top-level.nix:352:68:

          351|         config.system.copySystemConfiguration
          352|         ''ln -s '${import ../../../lib/from-env.nix "NIXOS_CONFIG" <nixos-config>}' \
             |                                                                    ^
          353|             "$out/configuration.nix"
(use '--show-trace' to show detailed location information)

I’ve run into some troubles learning how Nix works.

Specifically, the issue I’ve run into during the process is understanding what attributes are going to be passed to what functions, and what certain attribute values are expected to be, what keys they should have. For example, outputs.nixosConfigurations.<hostname> contains whatever nixpkgs.lib.nixosSystem decides to spit out. Can I please have a description of the attribute set that this will create? Maybe a description of the relationship between input and output? If it is available, I haven’t found a link for it, and I’ve scoured the website and wiki for documentation like this. Does it exist?

1 Like

Please disable/remove system.copySystemConfiguration, it doesn’t really work with flakes in its current form.

2 Likes

@spikespaz, I feel your pain. I’ve been working with nixOS for more than a year now and I still haven’t got my head around how to use the language. A key part I haven’t figured out is how to inspect the intermediaries – how to see what is passed from one function into another.

With regard to flakes and purity, I think it comes down to all inputs must be traced to a source which is hashed, such as a git repo. Inputs from the environment (such as <nixos-config>) are impure because they cannot be tracked.

1 Like

builtins.trace can help with that.

1 Like

Note that you can get similar (actually better) behavior than system.copySystemConfiguration with a flake by storing a path to the self flake input, like in my config here.

To fully answer that question, I’d need to explain a fair amount about how the nixos module system works, but in short, it pretty much just contains the final merged config, options, and other such attributes from the module system merge process. You can check out the actual value with nix eval .#nixosConfigurations.hostname --apply builtins.attrNames, and explore from there by specifying deeper attributes and/or altering the applied function.

Some useful things to know:

  • config.system.build.top-level is your entire nixos generation. It’s what nixos-rebuild actually builds (which you can verify for yourself, nixos-rebuild is just a bash script, after all)
  • More generally, config.foo.bar is the final merged value of the foo.bar option in your nixos config.
  • pkgs is the actual pkgs value presented as an argument to nixos modules, including config and overlays and such.
4 Likes