How to compose nixos configuration files?

I want to compose my configuration files so that my multiple machines can re-use the configuration I set up with code duplication.

What I’m imagining:

base.nix – sets up everything shared by all my computers
desktop-environment.nix – sets up what my standard desktop environment is
<machine_name>.nix – inherits from base.nix and desktop-environment.nix

The thing is, I want to define my user in base.nix, and then add additional groups to the user in my desktop-environment.nix or machine_name.nix files.

I’m new to the nix language and couldn’t figure out how to do this from the nix-pills on the wiki… Does anyone here know where I can find the documentation or an example of this syntax?

You can use modules to achieve that.

In my home-manager repo I do exactly that.

I just load a lot of modules and through host specific files (symlinked as hosts/default.nix) I specify settings for a certain host.

https://gitlab.com/NobbZ/nix-home-manager-dotfiles

I’m pretty sure similar techniques can be used for nixOS system level configuration.

I have my configuration files split up very similarly to what you’re describing based on this section of the manual.

My main configuration.nix on each machine imports my hosts/.nix file, which in turn imports the common configuration from common.nix, desktop configuration from desktop.nix for machines I plug into a monitor, etc.

I also use home-manager and would definitely recommend it, but I only use it for setting up dotfiles.

this is what the imports section is for, you can see an example at GitHub - NixOS/nixos-hardware: A collection of NixOS modules covering hardware quirks. . In your particular case, you will probably want to do something like this in your configuration.nix:

let
  myconfigs = fetchurl {...};
in
{ pkgs, lib }:
{
  imports = [
   myconfigs/base.nix
   desktop-environment.nix
   ...
  ];
}