A way to override a NixOS config parameter when buliding the system?

Is there a way to have a oneliner that builds a NixOS system with a parameter being overridden?

Currently I am building the system with something akin to:

nix-build --no-out-link /path/to/system --attr system

Where the nix expression looks like:

let
  pkgs = import ../nixpkgs { };
in import "${pkgs.pkgsSrc}/nixos" {
  system = "x86_64-linux";

  configuration = {
    networking.hostName = "...";
    nixpkgs.pkgs = pkgs;
    imports = [ ... ];
  };
}

pkgsSrc is just an attribute I added to nixpkgs that contains the source.

Ideally there would be something like

nix-build --no-out-link -E '(import /path/to/system).overrideConfig(_cfg: { services.bla.enable = true; }' --attr system

Thanks for the help!

I guess there is none then.

I have been using overrideAttrs today but I’m not sure what overrideConfig does. I’m wondering if you could use overrideAttrs.

1 Like

I think you could make your expression take an optional argument?

{ extraConfig ? {} }:

let
  pkgs = import ../nixpkgs { };
in import "${pkgs.pkgsSrc}/nixos" {
  system = "x86_64-linux";

  configuration ={
    networking.hostName = "...";
    nixpkgs.pkgs = pkgs;
    imports = [ extraConfig ];
  };
}

Then you could pass it with nix-build --arg extraConfig 'your-nix-expression-here'.
To override things that are predefined, you would need to play around with mkForce/ mkPriority (etc.)

I’m sure there’s better way to achieve this, but I believe this is a pretty simple solution.

1 Like

overrideConfig is just my made up name to something I wish existed.

overrideAttrs would override stuff at the derivation level if I understand correctly.
That sounds quite complicated when it comes to the derivation of a NixOS system, especially for a one-liner.

Thanks!

Can’t believe I hadn’t thought of such a simple thing.

I can’t imagine yet how big or small the one-liner will look but I will try this out.

Thanks!

1 Like

One way to parameterize NixOS configuration is to use NIX_PATH elements. For example:

$ nixos-rebuild -I my-file="/path/to/file.nix"
# configuration.nix
{  # ...
   imports =
     [  # ...
       <my-file>
     ];
}

And the my-file can be set to any string value (if I’m not mistaken) so it does not need to represent paths only. So that configuration may conditionally depend on the value given when evaluating.