Is it possible to edit nix as json using jq? or other tools?

How would you handle the following scenario?:
Note: it looks ugly but it’s what I came up with.

When I nixos-install my flake based nixos-config by running a setup-bashscript(partitioning,…). I have to set a nixosSystem’s specialArgs variable use-agenix to false before running nixos-install. This variable is used inside the module to disable certain parts because agenix can’t be used before there is a ssh-key and it’s added and re-keyed.

  • user password hash vs empty
  • wifi passwords vs disable wifi
  • mail password vs disable host status mails

All running hosts have it set to true and use agenix.

I could do:

  sed 's| use-agenix = true;| use-agenix = false;|g' -i "/mnt/nixcfg/flake.nix"

But there are multiple entries/hosts. I could change them all and later overwrite it again or bash my way through the file and find the use-agenix for that specific hostname and change it.
Is there an easy bash way to do this?
Is it possible/safe to read/edit it as json?

......
nixosConfigurations = { } // mkSystem rec {
  inherit overlays;
  hostname = "sl-think";
  system = "x86_64-linux";
  stateVersion = "22.05";
  use-agenix = true;
  modules = [
    {
    }
  ];
} // mkSystem rec {
  inherit overlays;
  hostname = "host-12345";
  stateVersion = "22.11";
  use-agenix = true;
  modules = [
    {
    }
  ];
} // mkSystem rec {
  inherit overlays;
  hostname = "host-abc";
  stateVersion = "22.11";
  use-agenix = true;
  modules = [
    {
    }
  ];
} // mkSystem rec {
  inherit overlays;
  hostname = "host-zzzz";
  stateVersion = "22.11";
  use-agenix = true;
......
1 Like

How about just being declarative about the two configurations (the fresh install and the full system)?

let
  host-abc = use-agenix: mkSystem { ... };
in
rec {
  host-abc = host-abc true;
  host-abc-install = host-abc false;
}

Install with: nixos-install --flake .#host-abc-install ...

And later upgrade with: nixos-rebuild switch --flake .#host-abc

(You could use importJSON with jq or nix eval --expr to automate generating code for nix, but imho this could be a lot of effort to go through for not much reward…)

2 Likes

good idea
ty I’ll try to let the mkSystem make them both

1 Like

I landed here because I just wrote a very similar sed expression to @mackenzie and found myself wishing for a nix-equivalent of jq. Probably @rehno’s suggestion is a good one for me as well–don’t rewrite files, just use nix directly.

I was writing a script to take a freshly installed nixos and modify it such that a flake in git somewhere now defines its configuration.

I was just hoping to get a sanity check on the conclusions that this thread lead me to:

Instead of:

  1. boot to installation media
  2. graphical install
  3. change hostname
  4. add entry to flake to include hostname
  5. sudo nixos-rebuild switch --flake .#hostname

I could instead:

  1. add entry to flake, indexed by hostname
  2. boot to installation media
  3. use nixos-install to install the system defined by that flake

Is that so? That’s a lot cleaner than my approach. If anyone knows a doc or somesuch that might help me on this path, I’d be interest to know about it. Thank you.