Declarative system configuration used on my 2019 Intel Macbook Pro.
$ nix-info -m && date
- system: `"x86_64-darwin"`
- host os: `Darwin 22.5.0, macOS 10.16`
- multi-user?: `yes`
- sandbox: `no`
- version: `nix-env (Nix) 2.15.1`
- nixpkgs: `/nix/store/jlbslziyjj8clk62y4pr7fc23amm5npp-source`
Sat Jun 10 21:15:00 PDT 2023
NixOS allows users to define the entire system configuration in a declarative manner using the Nix expression language. This includes specifying packages, services, users, and other system settings. The declarative nature of NixOS ensures that the system configuration is version-controlled, reproducible, and easy to share. It also simplifies system administration by providing a single source of truth for the entire system state.
Outside of NixOS, declarative system configuration can be achieved on MacOS through various Nix-based tools like home-manager
and nix-darwin
. The following tools work with both standard and flake
-based approaches:
System Configuration | Platforms | Configuration File | Configuration Options | Notes |
---|---|---|---|---|
NixOS | NixOS | configuration.nix |
Available in Manual or NixOS Options Search | |
Home-Manager | NixOS, Linux, MacOS | home.nix |
accounts , home , launchd , nix , programs , services , systemd , targets.darwin , wayland , xdg , xsession |
Can also be configured as NixOS module or nix-darwin module |
Nix-Darwin | MacOS | darwin-configuration.nix |
enviroment , homebrew , launchd , networking , nix , programs , services , system , users |
dotnix
is a flake-based configuration powered by Home-Manager.
File | Purpose | Notes |
---|---|---|
home.nix |
My system configuration via home-manager including: • apps (1Password, Firefox, VSCode) • dev settings ( .git , .bashrc )• CLI tools ( bat , fzf , jq )...and more |
• Home-Manager Manual - tool for declaratively managing system configuration, dotfiles, etc • Home-Manager Options & Options Search - pre-defined configurations available with home-manager • NixPkgs - package repository + binary cache with 100k+ available packages |
flake.nix |
Takes Nix expressions as input, then output things like package definitions, development environments, or, as is the case here, system configurations. For this specific repository, we can think of it as wrapping home.nix in order to provide it pinned dependencies and manage the outputs. |
• Zero to Nix Glossary • xeiaso's Nix Flake Guides |
flake.lock |
Pins dependencies used in flake inputs | |
bin/apply-system.sh |
Script to apply system configuration | Simple home-manager switch... invocation |
bin/update-system.sh |
Script to update dependencies | Simple nix flake update invocation |
Software installed with dotnix
is specified in following ways:
- Home-Manager Options
- Via
programs.*
inhome.nix
- Via
- Nixpkgs Package Set
- Via
home.packages
inhome.nix
, corresponding tonixpkgs
release atflake.nix:inputs.nixpkgs.url
- Via
- Flake Inputs
- Via
inputs
inflake.nix
and pinned inflake.lock
- Via
System-wide nix
settings are specified in home.nix
under the following declarations:
nix.package
- Determines which version of
nix
is used- Has matching declaration under
home.packages.config.nix.package
- Has matching declaration under
- Determines which version of
home.sessionVariables
- Environment variables set at login
nix.settings
- Replaces configuration that's usually found at
/etc/nix/nix.conf
- Sets feature flags, binary cache keys and locations, etc
- Replaces configuration that's usually found at
nix.registry
- Replaces configuration that's usually found at
/etc/nix/registry.json
- Same as default found at https://github.com/NixOS/flake-registry
-
"The flake registry serves as a convenient method for the Nix CLI to associate short names with flake URIs, such as linking
nixpkgs
togithub:NixOS/nixpkgs/nixpkgs-unstable.
"
-
- Replaces configuration that's usually found at
- Install
nix
Follow the Zero-to-Nix Quickstart Guide for a flake-based nix
installation.
- Setup
dotnix
repo
$ mkdir -p ~/.config
$ cd ~/.config
$ git clone git@github.com:hkailahi/dotnix.git
- Apply system configurations
$ cd ~/.config/dotnix/
$ ./bin/apply-system.sh
- Modify configuration files
For example, add a new package to install in home.nix
or move to a newer release of nixpkgs
in flake.nix
.
- Apply system configurations
$ cd ~/.config/dotnix/
$ ./bin/apply-system.sh
- Update dependencies in
flake.lock
$ cd ~/.config/dotnix/
$ ./bin/update-system.sh
- Apply system configurations
$ cd ~/.config/dotnix/
$ ./bin/apply-system.sh
This is the path I took for developing this repo following https://julomeiu.is/tidying-your-home-with-nix/.
- Install
nix
Follow the Zero-to-Nix Quickstart Guide for a flake-compatible nix installation
- Configure repo
Using
~/.config/dotnix
instead of~/.config/nixpkgs
$ mkdir -p ~/.config/dotnix
-
Setup up flake.nix and home.nix
-
Run
nix run .#homeConfigurations.hkailahi.activationPackage
to install home-manager and setup first configuration
After this, home-manager switch --flake .#hkailahi
can be run to switch home profile (aka rebuild home env via flake.nix and home.nix)
- This is just a shorthand for above
nix run .#homeConf....
NOTE: The .
in .#hkailahi
is the . Since I'm running from dotnix
repo, .
works, otherwise ~/.config/dotnix
or similar prob
Per https://nix-community.github.io/home-manager/index.html#ch-nix-flakes
The flake inputs are not upgraded automatically when switching. The analogy to the command home-manager --update ... is nix flake update.
If updating more than one input is undesirable, the command nix flake lock --update-input can be used.
You can also pass flake-related options such as --recreate-lock-file or --update-input [input] to home-manager when building/switching, and these options will be forwarded to nix build. See the NixOS Wiki page for detail.
Declarative macOS Configuration - Using nix-darwin
And home-manager
: https://xyno.space/post/nix-darwin-introduction
Setting up Nix on macOS: https://davi.sh/til/nix/nix-macos-setup/