repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
nix.mdMarkdown276 lines17.8 KiBrendered raw download
# nix — rebuilding this machine from ~/NixDaemon

NixOS **and** home-manager are one flake: `~/NixDaemon` (`NH_FLAKE` points at it).
home-manager runs as a NixOS module, so **one rebuild does both**; there is no separate `home-manager switch` here.
Terminal card: **`nix-cheat`** — sections `layout rebuild remote nh home search update rollback clean inspect add desktop dotfiles secrets repo shell`.

## layout — what lives where

Dendritic: `flake.nix` is `flake-parts.lib.mkFlake … (import-tree ./modules)`; every `*.nix` under `modules/` is a
flake-parts module that declares what it owns, and modules name each other through `self` (never by path).

```text
~/NixDaemon/flake.nix                 inputs only · outputs = import-tree ./modules
modules/parts.nix                     systems, the home-manager + wrapper-modules flake modules
modules/hosts/laptop/default.nix      flake.nixosConfigurations.nixos (modules = [ self.nixosModules.laptop ])
modules/hosts/laptop/configuration.nix  nixosModules.laptop: imports laptop-* + features by name; daemon.desktop.* switches
modules/hosts/laptop/*.nix            nixosModules.laptop-<name>: hardware, nvidia, ssd, nix-settings (nh), sops, toolbox, fan-*
modules/features/workstation.nix      nixosModules.workstation (Claude, Obsidian, gh, glab)
modules/features/home-manager.nix     nixosModules.home-manager: HM as a NixOS module → self.homeModules.daemonsec
modules/features/desktop/options.nix  daemon.desktop.hyprland.enable · daemon.desktop.niri.enable
modules/features/desktop/niri.nix     packages.niri (wrapped) + nixosModules.desktop-niri
modules/features/desktop/noctalia.nix packages.noctalia (wrapped, Rosé Pine)
modules/home/default.nix              homeModules.daemonsec: imports every homeModules.<name>
modules/home/tools.nix                packages: the toolbox closure and the general CLI tools  ← add packages here
modules/home/dotfiles.nix             every dotfile from dotfiles/ in this repo, as symlinks  ← add dotfile paths here
modules/home/hyprland.nix, hypr/*.lua Hyprland config + helpers (gated on daemon.desktop.hyprland)
modules/home/caelestia.nix            the Caelestia shell, its CLI, wallpaper dir (same gate)
modules/hosts/laptop/sops.nix         sops-nix (system) · modules/home/sops.nix (user) · secrets/secrets.yaml · .sops.yaml
```

## rebuild — nixos-rebuild, the plain way

```sh
cd ~/NixDaemon
sudo nixos-rebuild switch --flake .#nixos      # build, activate now, make it the boot default
sudo nixos-rebuild boot   --flake .#nixos      # build, activate on next boot only (kernel/driver changes)
sudo nixos-rebuild test   --flake .#nixos      # activate now, NOT the boot default (try something out)
nixos-rebuild build       --flake .#nixos      # just build → ./result, no sudo, nothing changes
sudo nixos-rebuild switch --flake ~/NixDaemon#nixos   # same, from any directory
```

- `#nixos` is the configuration name (= hostname) and the only one in the flake (the old `#bootstrap` stage is gone).
- **New files must be known to git** or the flake does not see them: `git add path` first (see *repo*).
- A rebuild activates home-manager too; its log: `journalctl --user -u home-manager-daemonsec -b` or `systemctl status home-manager-daemonsec`.

## remote — build straight from the GitLab repo

The flake does not have to be checked out: any `nix` command takes a **flake reference** to the repo.
The ssh form needs your key registered on GitLab (https works for the public repo); `?ref=main` pins a branch, `?rev=<sha>` a commit.

```sh
REPO='git+ssh://git@gitlab.com/DAEMON-404/NixDaemon.git'
sudo nixos-rebuild switch --flake "$REPO#nixos"                  # this machine, from the pushed main
sudo nixos-rebuild boot   --flake "$REPO?ref=main#nixos"
nh os switch "$REPO"                                             # nh takes the same reference
nix build "$REPO#nixosConfigurations.nixos.config.system.build.toplevel" --no-link --print-out-paths
nix flake show "$REPO"                                           # what the repo exports
nix flake metadata "$REPO"                                       # which commit nix resolved
# root (sudo) fetches with root's ssh: either run the fetch as you first (nix build …, cached), or give
# root the key via /root/.ssh/config, or use an https token in ~/.config/nix/netrc (machine gitlab.com …).
```

**Fresh machine, from the live ISO** (disk already partitioned and mounted at /mnt):
```sh
sudo nixos-generate-config --root /mnt --show-hardware-config > hardware-configuration.nix   # compare with modules/hosts/laptop/hardware.nix
git clone git@gitlab.com:DAEMON-404/NixDaemon.git ~/NixDaemon && cd ~/NixDaemon   # ssh key first (see *secrets*)
# paste its body into modules/hosts/laptop/hardware.nix (inside the laptop-hardware wrapper; a raw
# hardware-configuration.nix under modules/ would be loaded by import-tree as a flake-parts module), then:
sudo nixos-install --flake .#nixos
```
Afterwards (the dotfiles are in the repo, under `dotfiles/`): restore the age key to
`~/.config/sops/age/keys.txt` and `/var/lib/sops-nix/key.txt` (see *secrets*), and the toolbox to `~/.local/bin`.

A remote reference is fetched fresh each time (nix caches it briefly, `--refresh` forces it); the local
checkout at `~/NixDaemon` (`NH_FLAKE`) is the default for day-to-day work.

## nh — the same, with a diff and a progress tree

`nh` wraps nixos-rebuild: builds with nix-output-monitor, shows an **nvd diff** of what changes, then asks for sudo.

```sh
nh os switch                 # = sudo nixos-rebuild switch --flake ~/NixDaemon (NH_FLAKE), config picked by hostname
nh os boot                   # build + boot default, activate on reboot
nh os test                   # activate now, not the boot default
nh os build                  # build only, no activation, no sudo
nh os switch --dry           # show what would happen, do nothing
nh os switch --ask           # show the diff, then confirm before activating
nh os switch -u              # `nix flake update` first, then switch (updates EVERY input — see *update*)
nh os info                   # list system generations
nh os rollback               # go back one generation (see *rollback*)
nh os switch -- --show-trace # anything after -- goes to nix build (debug an eval error)
```

## home — home-manager in this setup

- home-manager is **inside** the NixOS build (modules/features/home-manager.nix: user `daemonsec` → `self.homeModules.daemonsec`, i.e. modules/home/).
  **`nh os switch` rebuilds it**; `nh home switch` / `home-manager switch` do not apply here (no standalone HM profile).
- Did my home change apply? `ls -l ~/.config/<thing>` (links point into /nix/store or ~/NixDaemon/dotfiles),
  `systemctl --user status home-manager-daemonsec`, or `journalctl --user -u home-manager-daemonsec -b`.
- HM backs up a file it has to replace as `*.hm-bak` (`backupFileExtension` in modules/features/home-manager.nix). Delete the backup once happy.
- Only build the home part (fast check, no sudo):
  `nix build ~/NixDaemon#nixosConfigurations.nixos.config.home-manager.users.daemonsec.home.activationPackage`

## search — finding packages and options

```sh
ns                          # fuzzy search everything indexed: nixpkgs packages, NixOS and home-manager options
ns kitty                    # start with a query
```
Inside `ns` (fzf): type to filter, ↑↓ move, the right pane shows the description, version, homepage and
the option's default. **Enter** prints the attribute name (paste it into `home.packages` in tools.nix),
**ctrl-y** copies it, **ctrl-o** opens the homepage, **ctrl-s** the nixpkgs source, **Esc** quits.
The index is built by `nix-search-tv` on first use (a few seconds) and refreshed by itself; by hand:
```sh
nix-search-tv print | head                 # the raw list   ·  nix-search-tv preview firefox   # one entry
nh search firefox                          # search.nixos.org from the terminal (needs network)
nix search nixpkgs firefox                 # nix's own search (slow first run: builds an eval cache)
nix eval ~/NixDaemon#nixosConfigurations.nixos.pkgs.firefox.version          # version THIS flake would install
nix shell nixpkgs#firefox                  # try it without installing  ·  nix run nixpkgs#cowsay -- hi
```

## update — moving the inputs

```sh
cd ~/NixDaemon
nix flake update                       # all inputs → flake.lock (nixpkgs, home-manager, hyprland, caelestia, llm-agents)
nix flake update nixpkgs home-manager  # only these inputs
nix flake update hyprland              # Hyprland alone (uses hyprland.cachix.org, so usually no compile)
nix flake lock                         # (re)write the lock without updating
nix flake metadata                     # which revisions are locked right now
nh os boot                             # then build it; boot = safest for kernel/driver bumps
```

Hyprland and Caelestia pin each other (flake.nix comments): update `hyprland` and `caelestia-*` together if the shell breaks.

## rollback — when a generation misbehaves

```sh
nh os rollback                          # previous generation, now
sudo nixos-rebuild switch --rollback    # the same, plain
nh os info                              # generation numbers
sudo nix-env --switch-generation 5 -p /nix/var/nix/profiles/system && sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch
```

- At power-on the **systemd-boot menu** lists every generation: pick an older one, it boots as it was.
- A broken Hyprland login: Ctrl+Alt+F2 for a console, then one of the above.

## clean — store and generations

```sh
nh clean all --keep 5 --keep-since 14d  # what the weekly timer runs (nix-settings.nix): drop old generations, GC
nh clean all --dry                      # show what it would remove
sudo nix-collect-garbage -d             # the blunt version: delete all old generations, then GC
nix store gc                            # GC only (nothing referenced by a generation is touched)
du -sh /nix/store                       # how big is it
```

## inspect — see before you switch

```sh
nh os build && nvd diff /run/current-system result     # what a switch would change
nix build ~/NixDaemon#nixosConfigurations.nixos.config.system.build.toplevel --no-link --print-out-paths
nix eval ~/NixDaemon#nixosConfigurations.nixos.pkgs.uv.version                 # version a package would get
nix eval ~/NixDaemon#nixosConfigurations.nixos.config.programs.zsh.enable      # read an option value
ns <query>                              # fuzzy search nixpkgs + NixOS/HM options, with descriptions (nix-search-tv)
nh search <package>                     # nixpkgs search (search.nixos.org)
nix search nixpkgs <package>            # local search (first run builds an index)
nix shell nixpkgs#<package>             # try a tool without installing it
nix run nixpkgs#<package> -- --help
nix flake check ~/NixDaemon             # evaluate every output
nix flake show ~/NixDaemon
```

## add — packages, options, dotfiles, modules

- **A package for the user**: `modules/home/tools.nix` → `home.packages` list → `nh os switch`.
- **A system package / service**: `modules/hosts/laptop/configuration.nix` (`environment.systemPackages`, `services.*`).
- **A dotfile from `dotfiles/`**: `modules/home/dotfiles.nix` → add its path to the list → `nh os switch`.
- **A new module from a template**: `nixmod new NAME` (or just `nixmod NAME`) → `enable = true;`, editable
  settings wrappers, and commented examples from the pinned Home Manager / NixOS module. Set values without
  editing braces: `nixmod new gallery-dl --set 'settings.extractor.base-directory=~/Downloads/gallery-dl/'`.
  New program modules evaluate the selected host automatically; use `nixmod check NAME` after later edits.
  `-t script|service|packages|config|feature|package` for the other
  templates, `--nixos` for a system module; `nixmod templates` lists them, `-n` previews. It is `git add`ed and named
  in the imports list. `nixmod rm NAME` deletes one again (file, import lines, git; `-n` to preview, only what
  nixmod made unless `--force`). `nixmod list`, `nixmod search fire`, `nixmod doc git` (defaults, examples,
  upstream source links). `nixmod-cheat` covers the full workflow. [Nixmod guide](../../../docs/nixmod.md).
- **A new home module by hand**: `modules/home/<name>.nix` declaring `flake.homeModules.<name> = { pkgs, ... }: { … };`,
  then `<name>` in the imports list of `modules/home/default.nix`. A NixOS one: `flake.nixosModules.<name>` in
  `modules/features/<name>.nix`, named in `configuration.nix`. `scripts/wrap.sh FILE ATTR` wraps a plain module file.
- **A Hyprland bind**: `modules/home/hypr/bindings.lua` (user) or `defaults.lua` (stock); a helper script goes in `hyprland.nix`.
- **A Niri bind**: `modules/features/desktop/niri.nix` → `settings.binds` (validated at build: a typo fails `nix build .#niri`).
- **A Caelestia setting**: `modules/home/caelestia.nix` → `programs.caelestia.settings`.
- **A Noctalia setting**: tweak it in the GUI, `dump-noctalia-shell`, paste the differing keys into `noctalia.nix` → `settings`.
- Then: `git add` anything new, `nh os switch`, and for Hyprland `hyprctl reload`. A running Niri keeps its old
  store config until you log in again, or: `niri msg action load-config-file --path "$(nix eval --raw ~/NixDaemon#niri)/niri-config.kdl"`.

## desktop — Hyprland + Caelestia or Niri + Noctalia

Both are installed; tuigreet lists both sessions and remembers the last one, so switching is log out → pick the other.

```sh
nix run ~/NixDaemon#niri          # try Niri nested in the current desktop (Alt is the modifier); Noctalia starts inside it
nix run ~/NixDaemon#noctalia      # the bar alone
```
```nix
# modules/hosts/laptop/configuration.nix — set one to false and `nh os switch` to drop it entirely
daemon.desktop = { hyprland.enable = true; niri.enable = true; };
```

## dotfiles — `dotfiles/` in the repo is the source of truth

- `~/NixDaemon/dotfiles/home/` is mirrored into `$HOME` by **symlinks** (dotfiles.nix): edit a file there and the live config changes at once — no rebuild.
- A rebuild is needed only to add/remove a *path* in dotfiles.nix. `ls -l ~/.config/starship.toml` shows where a link goes.
- zsh: `~/.dotfiles/config/*.zsh` → open a new shell (`exec zsh`). Plugins are vendored in `~/.dotfiles/config/plugins`.
- Not linked on purpose (hypr, kitty, git, mimeapps, bash rc, .claude …): the list and reasons are at the top of dotfiles.nix.
- Private bits are sops files, not dotfiles: jj identity, uncover API keys, and `agent-state` (Claude/Codex settings + memories: `agent-state list|restore|snapshot`).

## secrets — sops-nix and secretspec

Two tools, two jobs. **sops-nix**: secrets that belong in this repo, encrypted with **age**, decrypted at
activation to `/run/secrets/NAME` (system) or `~/.config/sops-nix/secrets/NAME` (user).
**secretspec**: runtime secrets for a *project*, declared in its `secretspec.toml`, stored in the keyring.

```sh
# sops — the one key: ~/.config/sops/age/keys.txt (user) = /var/lib/sops-nix/key.txt (system, root copy)
sudo install -D -m 600 -o root -g root ~/.config/sops/age/keys.txt /var/lib/sops-nix/key.txt   # once per machine
age-keygen -y ~/.config/sops/age/keys.txt         # the public key (recipient) in .sops.yaml
sops secrets/secrets.yaml                          # edit: opens decrypted in $EDITOR, re-encrypts on save
sops set secrets/secrets.yaml '["wifi-psk"]' '"hunter2"'   # add/replace one value without the editor
sops -d secrets/secrets.yaml                       # print decrypted
sops -d --extract '["example"]' secrets/secrets.yaml
sops updatekeys secrets/secrets.yaml               # after editing the recipients in .sops.yaml
```

Then declare it and rebuild:
```nix
# modules/hosts/laptop/sops.nix (system)                     # modules/home/sops.nix (user)
sops.secrets.wifi-psk = { };                         sops.secrets.my-token = { };
sops.secrets."ssd.key".path = "/etc/secrets/ssd.key";   # e.g. the LUKS key instead of restoring it by hand
```
`nh os switch` → `/run/secrets/wifi-psk` (root, 0400; `owner = "daemonsec";` to read it as the user).
Check: `sudo ls -l /run/secrets/`, `systemctl --user status sops-nix` (user side), `journalctl -b | grep sops`.

```sh
# secretspec — in a project directory (config: ~/.config/secretspec/config.toml → keyring, profile default)
secretspec init                 # writes secretspec.toml (commit it; it holds names, never values)
secretspec add DATABASE_URL     # declare a secret (flags: --required/--default/--description)
secretspec check                # prompts for every missing value, stores it in the keyring
secretspec set NAME            # (re)store one value
secretspec run -- ./server      # run with the secrets in the environment
secretspec export               # print them for another tool (shell `eval`)
secretspec claude configure     # let Claude Code fetch its API credential through secretspec
```

## repo — committing ~/NixDaemon

The repo is **jj, colocated with git** (`.jj` + `.git`). Nix flakes only see files git knows about, and jj keeps git's index at `@-`, so a *new* file is invisible to nix until it is added:

```sh
cd ~/NixDaemon
git add modules/home/new.nix       # make nix see a new file (modified tracked files are seen as-is)
nh os build                        # or switch
jj status                          # jj snapshots the working copy
jj describe -m "what changed" && jj new      # seal it (the vault ritual) — or plain: git add -A && git commit
jjmsg -c                           # or let the message write itself from the diff (jjmsg: print · -d describe · -e edit · -c commit)
jj log                             # history
```

A dirty tree only prints `warning: Git tree '/home/daemonsec/NixDaemon' is dirty`; it still builds.

## shell — after a switch

- New packages are on PATH in a **new** shell (`exec zsh`); the login shell itself changes at the next login.
- Hyprland: `hyprctl reload` re-reads `~/.config/hypr/*.lua`. Caelestia: `caelestia shell -r` restarts the shell.
- Everything in the Nix profile: `ls /etc/profiles/per-user/daemonsec/bin`.