How to evaluate a nix value from a shell script?

Hi,

I have written some bash scripts that helps me to manage my nix configuration. Part of what the scripts do is to help me ssh into the hosts that are managed by nix. In my nix config I have specified the static IP addresses of some hosts. What would be a good way for my script to “know” what IP address the nix config specifies?

To illustrate, I chose a random example from Github. In this file there is an IP address specified

What would be an accurate and clean way to parse that from a shell script? I could parse it with grep and regex but perhaps I can somehow evaluate the nix config to return it?

You can. nix-instantiate --eval or nix eval can do this, though how depends on if you’re using a flake or not.

No flake (I think… been a while since I did this):

nix-instantiate --eval '<nixos/nixos>' -A config.path.to.option

Flake:

nix eval .#nixosConfigurations.hostname.config.path.to.option

Wow, that was super cool. So for the record, the following provides me with what I will use

nix eval .#nixosConfigurations.mediaclient.config.networking.interfaces.enp1s0.ipv4.addresses --json

1 Like