From f29ddff912ddcd58a4726736702b24655505ce09 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Sat, 17 Oct 2026 10:36:36 +0200 Subject: refactor: introduce nixos/default.nix The system prompt addition is to make it run `npins show ` rather than `npins show` when looking for a specific pin. `npins get-path` is mentioned because the model doesn't "know" it yet (the subcommand was introduced in 2025 Dec). --- nixos/.gitignore | 2 +- nixos/README.md | 6 ++-- nixos/default.nix | 57 ++++++++++++++++++++++++++++++++++ nixos/hosts/ev/metadata.toml | 1 - nixos/hosts/hamac/metadata.toml | 1 - nixos/hosts/tente/metadata.toml | 1 - nixos/profiles/workstation/default.nix | 15 +++++++-- nixos/profiles/workstation/llm.nix | 1 + nixos/rebuild | 38 ++--------------------- nixos/system.nix | 4 --- 10 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 nixos/default.nix delete mode 100644 nixos/hosts/ev/metadata.toml delete mode 100644 nixos/hosts/hamac/metadata.toml delete mode 100644 nixos/hosts/tente/metadata.toml delete mode 100644 nixos/system.nix (limited to 'nixos') diff --git a/nixos/.gitignore b/nixos/.gitignore index 06d39e8..b2be92b 100644 --- a/nixos/.gitignore +++ b/nixos/.gitignore @@ -1 +1 @@ -/hosts/*/result +result diff --git a/nixos/README.md b/nixos/README.md index 075f812..f39917d 100644 --- a/nixos/README.md +++ b/nixos/README.md @@ -31,10 +31,8 @@ We're assuming that you just installed NixOS by going through the [official inst ``` nixos-generate-config --dir . ``` -3. Rename the `configuration.nix` to `default.nix`. - Create a `metadata.toml` file with a `channel` field - where the value is the key of a channel pinned in `npins/sources.json`. - A new channel can be pinned with: +3. Rename the `configuration.nix` to `hosts//default.nix`, then add the host to `hostDefs` in the top-level `default.nix`. + The channel must be a key in `npins/sources.json`. A new channel can be pinned with: ``` nix-shell -p npins --run \ diff --git a/nixos/default.nix b/nixos/default.nix new file mode 100644 index 0000000..e5090db --- /dev/null +++ b/nixos/default.nix @@ -0,0 +1,57 @@ +let + sources = import ./npins; + + mkConfiguration = + _hostName: + { channel, module, ... }: + let + nixpkgsPath = sources.${channel}.outPath; + in + import "${nixpkgsPath}/nixos/lib/eval-config.nix" { + modules = [ module ]; + specialArgs = { + inherit sources nixpkgsPath; + configurationPath = module; + }; + }; + + mkConfigurations = + selectModule: hostDefs: + builtins.mapAttrs ( + hostName: hostDef: mkConfiguration hostName (hostDef // { module = selectModule hostDef; }) + ) hostDefs; + + mkSystems = builtins.mapAttrs (_: configuration: configuration.config.system.build.toplevel); +in +rec { + hostDefs = { + ev = { + description = "home server"; + channel = "nixos-small"; + module = ./hosts/ev/private.nix; + standaloneModule = ./hosts/ev/default.nix; + }; + + hamac = { + description = "laptop"; + channel = "nixos"; + module = ./hosts/hamac/private.nix; + standaloneModule = ./hosts/hamac/default.nix; + }; + + tente = { + description = "VPS"; + channel = "nixos-small"; + module = ./hosts/tente/default.nix; + standaloneModule = ./hosts/tente/default.nix; + }; + }; + + configurations = mkConfigurations (host: host.module) hostDefs; + + systems = mkSystems configurations; + + standaloneConfigurations = mkConfigurations (host: host.standaloneModule) hostDefs; + + standaloneSystems = mkSystems standaloneConfigurations; +} diff --git a/nixos/hosts/ev/metadata.toml b/nixos/hosts/ev/metadata.toml deleted file mode 100644 index 58f0301..0000000 --- a/nixos/hosts/ev/metadata.toml +++ /dev/null @@ -1 +0,0 @@ -channel = "nixos-small" diff --git a/nixos/hosts/hamac/metadata.toml b/nixos/hosts/hamac/metadata.toml deleted file mode 100644 index fc34e27..0000000 --- a/nixos/hosts/hamac/metadata.toml +++ /dev/null @@ -1 +0,0 @@ -channel = "nixos" diff --git a/nixos/hosts/tente/metadata.toml b/nixos/hosts/tente/metadata.toml deleted file mode 100644 index 58f0301..0000000 --- a/nixos/hosts/tente/metadata.toml +++ /dev/null @@ -1 +0,0 @@ -channel = "nixos-small" diff --git a/nixos/profiles/workstation/default.nix b/nixos/profiles/workstation/default.nix index 578cece..77891fe 100644 --- a/nixos/profiles/workstation/default.nix +++ b/nixos/profiles/workstation/default.nix @@ -1,4 +1,13 @@ -{ config, lib, pkgs, pkgs-unstable, sources, ... }: +{ + config, + configurationPath, + lib, + nixpkgsPath, + pkgs, + pkgs-unstable, + sources, + ... +}: let cfg = config.workstation; @@ -24,9 +33,9 @@ in config = { # Makes commands default to the same Nixpkgs, config, overlays and NixOS configuration nix.nixPath = [ - "nixpkgs=${toString }" + "nixpkgs=${toString nixpkgsPath}" "nixpkgs-unstable=${pkgs-unstable.path}" - "nixos-config=${toString }" + "nixos-config=${toString configurationPath}" ]; environment.variables.NIXPKGS_CONFIG = lib.mkForce (toString ./nixpkgs/config.nix); diff --git a/nixos/profiles/workstation/llm.nix b/nixos/profiles/workstation/llm.nix index a24a14c..a0dfc92 100644 --- a/nixos/profiles/workstation/llm.nix +++ b/nixos/profiles/workstation/llm.nix @@ -46,6 +46,7 @@ let --chdir "$physical_pwd" \ ${pkgs-unstable.pi-coding-agent}/bin/pi \ --append-system-prompt "You're running in a sandbox with an ephemeral root filesystem and a separate PID namespace. The current working directory and a few configuration and cache paths are mounted from the host." \ + --append-system-prompt "$HOME/.config/pi/APPEND_SYSTEM.md" \ "$@" ''; diff --git a/nixos/rebuild b/nixos/rebuild index 3c675b2..395f78d 100755 --- a/nixos/rebuild +++ b/nixos/rebuild @@ -3,49 +3,17 @@ set -euo pipefail top=$(dirname "$(realpath "$0")") hostDir="$top/hosts/$HOSTNAME" -defaultConfigPath="$hostDir/default.nix" -privateConfigPath="$hostDir/private.nix" -case "${1:-}" in - --default) - configPath="$defaultConfigPath" - shift - ;; - *) - if [ -f "$privateConfigPath" ]; then - configPath="$privateConfigPath" - else - configPath="$defaultConfigPath" - fi - ;; -esac - -if [ ! -f "$configPath" ]; then - echo "aborting: $configPath doesn't exist" +if [ ! -d "$hostDir" ]; then + echo "aborting: host directory $hostDir doesn't exist" exit 1 fi -nixpkgsPath=$( - cd "$top" && - nix-instantiate --eval --raw \ - --argstr host "$HOSTNAME" \ - --expr ' - { host }: - let - toml = builtins.readFile ./hosts/${host}/metadata.toml; - channel = (builtins.fromTOML toml).channel; - in - (import ./npins).${channel}.outPath - ' -) - ( # The result symlink created in the current directory is registered as a root of the Nix # garbage collector, so it's important that we call nixos-rebuild from the host directory. # (nix-build has an --out-link option but that option cannot be set via nixos-rebuild). cd "$hostDir" && - # nixos-rebuild always reads Nixpkgs from the NIX_PATH, - # so we need to set it explicitly to our pinned version - exec nixos-rebuild "$@" --file "$top/system.nix" -I nixpkgs="$nixpkgsPath" -I nixos-config="$configPath" + exec nixos-rebuild "$@" --file "$top/default.nix" --attr "configurations.$HOSTNAME" ) diff --git a/nixos/system.nix b/nixos/system.nix deleted file mode 100644 index a3b04cb..0000000 --- a/nixos/system.nix +++ /dev/null @@ -1,4 +0,0 @@ -import { - configuration = import ; - specialArgs.sources = import ./npins; -} -- cgit v1.3.1