i’ve seen the definitions of course, but i don’t feel like i fully understand what goes into building a NixOS configuration, as opposed to installing packages with Nix on some other OS.
is my NixOS configuration just another derivation like any other that i could potentially write in Nix?
does this derivation simply produce a lot of system init files in Nix Store, and then the nixos-rebuild-ng utility is needed on top of that to move those files to appropriate system locations?
or is it about more / something other than a bunch of system init files?
as an example (maybe it makes it easier for me to understand the boundaries), what would it take to build NixOS with some major part of it replaced, say, swapping systemd for GNU Shepherd? would i need to change some Nix code for the derivation? or would i need to change something on the nixos-rebuild-ng side? or is it not going to be NixOS at all anymore?
A NixOS configuration does eventually produce a derivation (actually many of them, but system.build.toplevel is the most important one that nixos-rebuild switch consumes).
However, the derivations are created through the module system.
I’m almost certain there are por
There are actually both nixos-rebuild-ng and switch-to-configuration-ng:
NixOS is heavily tied to systemd, and that’s not going to change. However, there is some experimentation in nixpkgs-based distros that don’t use systemd, for example @aanderse’s finix, as well as adjacent work in modular services, which AFAIK one of the goals of is to not be tied to one service manager.
NixOS instance is just a package with a ton of dependencies. Among those dependencies are kernel, initramfs, unit files… Also the scripts to put in place a few symlinks. I think NixOS is actually making the filesystem-level switch less atomic than it should be (my Nix-based system routes everything through a single current-system symlink, as far as I remember NixOS still walks /etc)
Importantly, from the Nix side this package is configured/evaluated/generated via the module system. Which is a bit of emulation of global namespace with spooky action at a distance in a purely functional language.
There are many alternative setups that generate different OS packages from Nixpkgs. Most of them use the module system, but this is not mandatory. (My system is single-main-user multiple-setups, so I just do everything overlay-style.)
Replacing systemd is more like porting a large application that is all about networking from OpenSSL to GNUTLS. Replacing the module system in NixOS completely is like porting a program from OCaml to Rust.
Nix is a layered system. It’s not currently as cleanly separated as it ideally could be (people are working on this) but… we might say that there is at least:
The store. A managed directory in your filesystem, which can hold files and folders keep track of where they came from, and push to and pull from servers remote server (with integrity checks)
The build system. Derivations (packages) and builders. It uses input-addressing (hashed, establishing a Merkle tree) to assign build outputs places in the store. It can also arrange to run builds on remote servers.
The nix language. A lazy, purely functional programming language, with some nice integrations with the store and builders.
The nixpkgs nix library. Essentially the standard library for the nix language, pulled in from the nixpkgs tree. It contains a lot of useful utilities. Notably, one of these utilities is the “module system”, which gives you a way of composing complex, very-configurable objects in the nix language.
The nixpkgs package set. The largest collection of open source packages around! Notably, it doesn’t really use the module system (I mean it does a tiny bit, to check the user’s nixpkgs package set configuration, but that’s nearly an implementation detail)
The NixOS Linux distribution. This is a big set of modules for the module system that live in the nixpkgs tree, with a focus on creating a working Linux distribution. In fact, it does produce a set of derivations on top of nixpkgs that represent the files needed by the system you’re trying to create. Like I said before, the module system produces configurable nix objects. These ones just happen to be derivations. The primary output is a derivation containing the tools needed to converge a real Linux system to run the config you’ve specified (“activation scripts”). It also produces derivations for things like VM images based on your config and scripts to run that VM image locally in qemu.
Currently, “nix” owns (1) through (3) and there are compatible alternate implementations, such as Lix. Then, (4) through (6) live together in the nixpkgs repo.
There are good answers here, but I’ll also note that Learn Nix | Nix & NixOS | Nix & NixOS has all 3 manuals for nix, Nixpkgs and NixOS (scroll down just a bit).