Skip to content

Commit e58841d

Browse files
committed
make user identity machine-agnostic via flake/env defaults
1 parent 013da19 commit e58841d

3 files changed

Lines changed: 217 additions & 82 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ Personal NixOS configuration using flakes, with categorized hosts (laptops, VPS,
4242
- Git
4343
- GPG key for secrets encryption
4444

45+
## Identity and Machine-Agnostic Defaults
46+
47+
This repo supports user identity overrides without editing module code.
48+
49+
- `NIXCFG_USER` (default: `nixos`)
50+
- `NIXCFG_GIT_NAME` (default: `NIXCFG_USER`)
51+
- `NIXCFG_GIT_EMAIL` (default: `<NIXCFG_USER>@localhost`)
52+
53+
Example:
54+
55+
```bash
56+
export NIXCFG_USER="$USER"
57+
export NIXCFG_GIT_NAME="Your Name"
58+
export NIXCFG_GIT_EMAIL="you@example.com"
59+
```
60+
61+
These values are consumed by flake outputs and Home Manager user module wiring.
62+
4563
## Quick Start
4664

4765
📹 **New to this setup?** Watch the [5-minute quick start tutorial](docs/tutorials/01-quick-start.cast) to see the fully automated installation in action.

flake.nix

Lines changed: 181 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
description = "NixOS configuration with categorized hosts and centralized deployment";
2+
description = "NixOS configuration with categorized hosts and multi-architecture support";
33

44
inputs = {
55
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
@@ -15,180 +15,285 @@
1515
url = "github:serokell/deploy-rs";
1616
inputs.nixpkgs.follows = "nixpkgs";
1717
};
18+
nixvim = {
19+
url = "github:nix-community/nixvim";
20+
inputs.nixpkgs.follows = "nixpkgs";
21+
};
1822
};
1923

20-
outputs = { self, nixpkgs, home-manager, sops-nix, deploy-rs, ... }@inputs:
24+
outputs = { self, nixpkgs, home-manager, sops-nix, deploy-rs, nixvim, ... }@inputs:
2125
let
22-
system = "x86_64-linux";
23-
pkgs = import nixpkgs { inherit system; };
26+
# Machine-agnostic identity defaults (override via env when needed).
27+
defaultUser =
28+
let u = builtins.getEnv "NIXCFG_USER";
29+
in if u != "" then u else "nixos";
30+
defaultGitName =
31+
let n = builtins.getEnv "NIXCFG_GIT_NAME";
32+
in if n != "" then n else defaultUser;
33+
defaultGitEmail =
34+
let e = builtins.getEnv "NIXCFG_GIT_EMAIL";
35+
in if e != "" then e else "${defaultUser}@localhost";
36+
37+
# Supported architectures
38+
supportedSystems = [
39+
"x86_64-linux" # Intel/AMD 64-bit
40+
"aarch64-linux" # ARM 64-bit (including Ampere)
41+
"x86_64-darwin" # Intel Mac
42+
"aarch64-darwin" # Apple Silicon (M1/M2/M3)
43+
];
44+
45+
# Helper to generate attribute sets for all systems
46+
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
2447

25-
# Helper function to create NixOS configurations
26-
mkHost = { hostname, category, extraModules ? [] }:
48+
# Per-system package sets
49+
pkgsFor = forAllSystems (system:
50+
import nixpkgs {
51+
inherit system;
52+
config.allowUnfree = true;
53+
}
54+
);
55+
56+
# Helper function to create NixOS configurations with architecture support
57+
mkHost = {
58+
hostname,
59+
category,
60+
system ? "x86_64-linux", # Default to x86_64-linux for backwards compatibility
61+
userName ? defaultUser,
62+
userGitName ? defaultGitName,
63+
userGitEmail ? defaultGitEmail,
64+
extraModules ? []
65+
}:
2766
nixpkgs.lib.nixosSystem {
28-
system = "x86_64-linux";
29-
specialArgs = { inherit inputs; };
67+
inherit system;
68+
specialArgs = {
69+
inherit
70+
inputs
71+
userName
72+
userGitName
73+
userGitEmail
74+
;
75+
};
3076
modules = [
3177
./hosts/${category}/${hostname}/configuration.nix
3278
home-manager.nixosModules.home-manager
3379
sops-nix.nixosModules.sops
3480
{
3581
home-manager.useGlobalPkgs = true;
3682
home-manager.useUserPackages = true;
37-
home-manager.users.giovanni = import ./modules/users/giovanni.nix;
83+
home-manager.users.${userName} = import ./modules/users/giovanni.nix;
84+
home-manager.sharedModules = [
85+
nixvim.homeManagerModules.nixvim
86+
];
3887
}
3988
] ++ extraModules;
4089
};
4190

42-
# Helper to create deploy-rs nodes
43-
# Uses Tailscale hostnames for opaque addressing
44-
mkDeployNode = { hostname, configName }:
91+
# Helper to create deploy-rs nodes with architecture awareness
92+
mkDeployNode = {
93+
hostname,
94+
configName,
95+
system ? "x86_64-linux", # Default to x86_64-linux
96+
sshUser ? defaultUser
97+
}:
4598
{
46-
# Use Tailscale hostname for maximum opacity
47-
# Format: hostname.tailnet-name.ts.net
48-
# Or just use the short name if configured in /etc/hosts or SSH config
49-
hostname = "${hostname}"; # Override in hosts file or use Tailscale name
99+
# Use SSH config hostname (managed by our ssh-config module)
100+
hostname = "${hostname}";
50101

51102
profiles.system = {
52103
user = "root";
53-
sshUser = "giovanni";
54-
path = deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.${configName};
104+
inherit sshUser;
105+
# Dynamically select the correct deploy-rs lib based on system
106+
path = deploy-rs.lib.${system}.activate.nixos self.nixosConfigurations.${configName};
55107
};
56108
};
57109
in
58110
{
59-
# NixOS Configurations organized by category
111+
# NixOS Configurations organized by category and architecture
60112
nixosConfigurations = {
61-
# === LAPTOPS ===
113+
# === LAPTOPS (x86_64) ===
62114
bit = mkHost {
63115
hostname = "bit";
64116
category = "laptops";
117+
system = "x86_64-linux";
65118
};
66119

67120
spark = mkHost {
68121
hostname = "spark";
69122
category = "laptops";
123+
system = "x86_64-linux";
70124
};
71125

72126
hermes = mkHost {
73127
hostname = "hermes";
74128
category = "laptops";
129+
system = "x86_64-linux";
75130
};
76131

77-
# === VPS ===
132+
# === LAPTOPS (ARM - Apple Silicon example) ===
133+
# Uncomment when you have ARM laptops
134+
# macbook = mkHost {
135+
# hostname = "macbook";
136+
# category = "laptops";
137+
# system = "aarch64-darwin";
138+
# };
139+
140+
# === VPS (x86_64) ===
78141
vps-alpha = mkHost {
79142
hostname = "example-vps";
80143
category = "vps";
144+
system = "x86_64-linux";
81145
};
82146

83-
# Add more VPS hosts here as needed
84-
# vps-beta = mkHost {
85-
# hostname = "vps-beta";
147+
# === VPS (ARM/Ampere - Example) ===
148+
# Uncomment when you have ARM-based VPS (e.g., Oracle Ampere, AWS Graviton)
149+
# vps-arm = mkHost {
150+
# hostname = "vps-arm";
86151
# category = "vps";
152+
# system = "aarch64-linux";
87153
# };
88154

89-
# === SERVERS ===
155+
# === SERVERS (x86_64) ===
90156
server-alpha = mkHost {
91157
hostname = "example-server";
92158
category = "servers";
159+
system = "x86_64-linux";
93160
};
94161

95-
# Add more servers here
96-
# server-beta = mkHost {
97-
# hostname = "server-beta";
162+
# === SERVERS (ARM - Example for Raspberry Pi, Ampere, etc.) ===
163+
# server-arm = mkHost {
164+
# hostname = "server-arm";
98165
# category = "servers";
166+
# system = "aarch64-linux";
99167
# };
100168

101169
# === EXPERIMENTS ===
102170
experiment-alpha = mkHost {
103171
hostname = "example-experiment";
104172
category = "experiments";
173+
system = "x86_64-linux";
105174
};
106175
};
107176

108177
# Deploy-rs configuration for remote deployments
109-
# Hostnames are intentionally opaque - use Tailscale or SSH config aliases
178+
# Architecture is automatically handled based on host system
110179
deploy.nodes = {
111-
# Laptops - typically deployed via Tailscale
180+
# Laptops (x86_64)
112181
bit = mkDeployNode {
113182
hostname = "bit";
114183
configName = "bit";
184+
system = "x86_64-linux";
115185
};
116186

117187
spark = mkDeployNode {
118188
hostname = "spark";
119189
configName = "spark";
190+
system = "x86_64-linux";
120191
};
121192

122193
hermes = mkDeployNode {
123194
hostname = "hermes";
124195
configName = "hermes";
196+
system = "x86_64-linux";
125197
};
126198

127-
# VPS - access via Tailscale or configure in ~/.ssh/config
199+
# VPS (x86_64)
128200
vps-alpha = mkDeployNode {
129201
hostname = "vps-alpha";
130202
configName = "vps-alpha";
203+
system = "x86_64-linux";
131204
};
132205

133-
# Servers
206+
# Servers (x86_64)
134207
server-alpha = mkDeployNode {
135208
hostname = "server-alpha";
136209
configName = "server-alpha";
210+
system = "x86_64-linux";
137211
};
138212

139213
# Experiments
140214
experiment-alpha = mkDeployNode {
141215
hostname = "experiment-alpha";
142216
configName = "experiment-alpha";
217+
system = "x86_64-linux";
143218
};
219+
220+
# Example ARM deployments (uncomment as needed)
221+
# vps-arm = mkDeployNode {
222+
# hostname = "vps-arm";
223+
# configName = "vps-arm";
224+
# system = "aarch64-linux";
225+
# };
144226
};
145227

146228
# Deploy-rs checks
147229
checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) deploy-rs.lib;
148230

149-
# Development shell with deployment tools
150-
devShells.${system}.default = pkgs.mkShell {
151-
buildInputs = with pkgs; [
152-
deploy-rs.packages.${system}.deploy-rs
153-
just
154-
nixos-anywhere
155-
git
156-
openssh
157-
sops
158-
age
159-
gnupg
160-
pre-commit
161-
knockd # For port knocking
162-
jq
163-
mkpasswd
164-
];
165-
166-
shellHook = ''
167-
echo "╔══════════════════════════════════════════════════════════╗"
168-
echo "║ NixOS Configuration Development Environment ║"
169-
echo "╚══════════════════════════════════════════════════════════╝"
170-
echo ""
171-
echo "Available Hosts:"
172-
echo " Laptops: bit, spark, hermes"
173-
echo " VPS: vps-alpha"
174-
echo " Servers: server-alpha"
175-
echo " Experiments: experiment-alpha"
176-
echo ""
177-
echo "Quick Start:"
178-
echo " just --list - Show all commands"
179-
echo " just install spark laptops IP - Install new host (zero manual steps)"
180-
echo " just deploy bit - Deploy to host"
181-
echo " just check - Validate configuration"
182-
echo ""
183-
echo "Installation Example:"
184-
echo " just install spark laptops 192.168.1.100"
185-
echo ""
186-
echo "Documentation:"
187-
echo " docs/WORKFLOW.md - Complete workflow guide"
188-
echo " docs/AUTOMATED_INSTALLATION.md - Zero-touch install guide"
189-
echo " docs/SOPS_GPG_SETUP.md - Secrets management"
190-
echo ""
191-
'';
192-
};
231+
# Development shells for all supported architectures
232+
devShells = forAllSystems (system:
233+
let
234+
pkgs = pkgsFor.${system};
235+
in
236+
{
237+
default = pkgs.mkShell {
238+
buildInputs = with pkgs; [
239+
deploy-rs.packages.${system}.deploy-rs
240+
just
241+
nixos-anywhere
242+
git
243+
openssh
244+
sops
245+
age
246+
gnupg
247+
pre-commit
248+
jq
249+
asciinema
250+
mkpasswd
251+
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
252+
knockd # For port knocking (Linux only)
253+
];
254+
255+
shellHook = ''
256+
echo "╔══════════════════════════════════════════════════════════╗"
257+
echo "║ NixOS Configuration Development Environment ║"
258+
echo "║ Architecture: ${system} ║"
259+
echo "╚══════════════════════════════════════════════════════════╝"
260+
echo ""
261+
echo "Available Hosts by Architecture:"
262+
echo " x86_64-linux:"
263+
echo " Laptops: bit, spark, hermes"
264+
echo " VPS: vps-alpha"
265+
echo " Servers: server-alpha"
266+
echo " Experiments: experiment-alpha"
267+
echo ""
268+
echo " aarch64-linux:"
269+
echo " (Add ARM hosts in flake.nix)"
270+
echo ""
271+
echo "Quick Start:"
272+
echo " just --list - Show all commands"
273+
echo " just install spark laptops IP - Install new host"
274+
echo " just deploy bit - Deploy to host"
275+
echo " just check - Validate configuration"
276+
echo ""
277+
echo "Adding ARM hosts:"
278+
echo " Edit flake.nix and set system = \"aarch64-linux\""
279+
echo ""
280+
echo "Documentation:"
281+
echo " docs/WORKFLOW.md - Complete workflow guide"
282+
echo " docs/MULTI_ARCH.md - Multi-architecture guide"
283+
echo " docs/SOPS_GPG_SETUP.md - Secrets management"
284+
echo ""
285+
'';
286+
};
287+
}
288+
);
289+
290+
# Expose package sets for all systems
291+
packages = forAllSystems (system: {
292+
# Expose useful packages per-system
293+
default = pkgsFor.${system}.hello;
294+
});
295+
296+
# Formatter for all systems
297+
formatter = forAllSystems (system: pkgsFor.${system}.nixpkgs-fmt);
193298
};
194299
}

0 commit comments

Comments
 (0)