Nix repl / system configuration - manipulate config

nix repl '<nixpkgs/nixos>'

config.services.xserver.displayManager.session = []
error: syntax error, unexpected '=', expecting end of file

       at «string»:1:48:

            1| config.services.xserver.displayManager.session =
             |                                                ^

How to manipulate configuration attributes (not for the system state but temporarily) in the repl ?

You can’t.

You generally only change your systems configuration through the configuration file and imported modules.

Hello Norbert,
thanks for the answer.
To clarify it.
The aim is not to change the system state/configuration but to change/test the config (as kind of basline) with certain changes applied to.

You can’t change the configuration. Best you can get is to use evalModule or how it is called to get the eventual config returned such that you can inspect it, but you can’t write back to it or build a slightly altered version from the repl.

Given this situation.
How does nixos user interactively change and test (their) configuration, in an efficient way?

They change the files and use nixos-rebuild test to avoid creating boot entries.

They also often use filesystem snapshots or git to make sure that they can revert to the “known to work” configuration.

This is somewhat overlapping with Nix repl # error: anonymous function at /home/a/configuration.nix:29:1 called without required argument 'config'. There is not really a nice interface for this because REPLs are not generally designed for mutation. You would need to use evalConfig as Sandro mentions in the other thread.

If you wanted to find out how to do it, you could start with nixos-rebuild – here you can see that it will try to build system attribute in the <nixpkgs/nixos> path:

Going through NIX_PATH, you will end up in nixos/default.nix file in Nixpkgs:

You can already see how it is put together, and if you want to evaluate the configuration with extra options, you can just pass them inside another module:

nix-repl> import ./nixos/lib/eval-config.nix {
            system = builtins.currentSystem;
            modules = [
              ./configuration.nix
          
              ({lib, ...}: {
                services.xserver.displayManager.session = lib.mkForce [];
              })
            ];
          }

{ _module = { ... }; _type = "configuration"; class = "nixos"; config = { ... }; extendModules = «lambda @ /home/jtojnar/Projects/nixpkgs/lib/modules.nix:301:23»; extraArgs = { ... }; options = { ... }; pkgs = { ... }; type = { ... }; }

Also note that lists are concatenated by default so if you want to clear out services.xserver.displayManager.session, you would need to use lib.mkForce.

1 Like