Can I pass argument to a Nix file?

I am currently using this way to switch windows manager. When I want to use Hyprland, I change it’s path.

Wondering if there is a way like imports = [ (./modules/graphical/wm sway) ]; to pass argument to this file and process it inside the one file.

I would say that passing arguments to nix is not the idiomatic way of doing this. It’s possible, but I would avoid it.

Instead, consider when you actually want to switch WMs:

  • Want to use a different one day-by-day as you feel like it? Install them all to use the xdg sessions and pick your WM in a display manager (gdm, lightdm, greetd…).
  • Want to make your dotfiles available for install by lots of people? Write a custom option and gate enabling each WM behind lib.mkIf, so that downstream users can choose what they prefer (and override and compose with the power of the module system).
  • Want to use different WMs on different computers? Make an entrypoint for each computer, and only import the correct WM configuration for the computer - then install using e.g. nixos-rebuild switch -I nixos-config=/etc/nixos/laptop.

I would really not reccommend using arguments; it just isn’t the right abstraction. Configurations should be mostly static, switching out modules and options works better. Using arguments will also make using evaluation caches trickier if you switch to flakes, assuming flakes ever end up supporting arguments in the first place.

Do you have some other use case where you think using arguments makes more sense?

After thinking… I would say the way now is probably enough for me.
For real usage, I won’t change WM so quickly - everyday.
But also, I want to make a configuration which is super flexible so that I can make preset changes easily as I want.

In that case you probably want to make custom options that contain these “presets”, which you can then turn on and off with just a single line code change.

1 Like

I think it’s on the point - optional modules with options!

1 Like