Nixos configuration.nix with argument

The configuration.nix file starts with

  , testing ? false 
  , ...

How to specify the parameter - e.g. testing=true - in the (shell) execution of nixos-rebuild ?

1 Like

You are searching for --arg testing true

nixos-rebuild does not understand --arg nor --argstr:

$ nixos-rebuild build --arg foo true                                                              /run/current-system/sw/bin/nixos-rebuild: unknown option `--arg'
1 Like

I do not think this is currently possible.¹ Even if nixos-rebuild supported --arg flag, the modules are likely too deep for Nix to pass them the arguments.

In theory, you could abuse NIX_PATH for passing stringly-typed arguments but that is super ugly and will not work in pure mode.

If you have just a single argument, I would go with creating a configuration-testing.nix file containing something like the following:

  imports = [
  _module.args = {
    testing = true;

And then invoke nixos-rebuild with -I nixos-config=path/to/configuration-testing.nix.nix argument.

  1. There is a Nix PR that might allow this in the future: Configurable derivations by edolstra · Pull Request #6583 · NixOS/nix · GitHub
1 Like