Skip to content

Commit deaa9c7

Browse files
committed
fix: nix profile add for the newer way plus updates
1 parent e2c5ef7 commit deaa9c7

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

ansible/tasks/setup-system-manager.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
- name: Install system-manager from binary cache
33
ansible.builtin.shell: |
44
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
5-
nix profile install --accept-flake-config "github:supabase/postgres/{{ git_commit_sha }}#system-manager"
5+
nix profile add --accept-flake-config "github:supabase/postgres/{{ git_commit_sha }}#system-manager"
66
become: true
77

88
- name: Build and activate system-manager config

nix/docs/system-manager.md

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,73 @@ It replaces imperative service setup with reproducible Nix module definitions, b
77

88
The AMI build uses a two-stage pipeline orchestrated by Packer and Ansible.
99
Stage 1 installs Nix itself, while stage 2 uses Nix to build and deploy all services.
10-
system-manager is deployed during stage 2 via the Ansible task `ansible/tasks/setup-system-manager.yml`:
10+
system-manager is deployed during stage 2 via the Ansible task `ansible/tasks/setup-system-manager.yml`.
11+
12+
### Installation
13+
14+
The `system-manager` binary is installed from the binary cache using the remote flake URL, pinned to the current git SHA:
1115

1216
```yaml
13-
- name: Deploy system manager
17+
- name: Install system-manager from binary cache
1418
ansible.builtin.shell: |
1519
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
16-
cd /tmp
17-
nix run --accept-flake-config /tmp/flake#system-manager -- switch --flake /tmp/flake 2>&1 | tee /tmp/system-manager-deploy.log
20+
nix profile add --accept-flake-config "github:supabase/postgres/{{ git_commit_sha }}#system-manager"
1821
become: true
1922
```
2023
21-
This sources the Nix daemon profile, then runs `system-manager switch` against the flake to apply the declared system configuration.
24+
### Activation
25+
26+
The system configuration is built from the remote flake URL (pulled from the binary cache), then registered and activated:
27+
28+
```yaml
29+
- name: Build and activate system-manager config
30+
ansible.builtin.shell: |
31+
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
32+
STORE_PATH=$(nix build --accept-flake-config --no-link --print-out-paths "github:supabase/postgres/{{ git_commit_sha }}#systemConfigs.$(nix eval --raw nixpkgs#system).default")
33+
system-manager register --store-path "$STORE_PATH" --sudo
34+
system-manager activate --store-path "$STORE_PATH" --sudo
35+
become: true
36+
```
37+
38+
No source tree is uploaded to the build instance. Both the `system-manager` binary and the system configuration are fetched as pre-built artifacts from the `nix-postgres-artifacts` S3 binary cache.
39+
40+
## Updating system-manager on a running instance
41+
42+
system-manager can be updated on a running instance without rebuilding the AMI. To apply a new configuration:
43+
44+
```bash
45+
# Build the new config (fetched from binary cache)
46+
STORE_PATH=$(nix build --accept-flake-config --no-link --print-out-paths \
47+
"github:supabase/postgres/<new-sha>#systemConfigs.$(uname -m)-linux.default")
48+
49+
# Register and activate
50+
system-manager register --store-path "$STORE_PATH" --sudo
51+
system-manager activate --store-path "$STORE_PATH" --sudo
52+
```
53+
54+
This pulls the pre-built configuration from the binary cache and activates it. system-manager diffs the old and new state and reconciles — starting, stopping, or restarting services and updating `/etc` entries as needed. No explicit deactivation is required.
55+
56+
To also update the `system-manager` binary itself (if the upstream version changed in `flake.lock`):
57+
58+
```bash
59+
nix profile upgrade --accept-flake-config system-manager
60+
```
61+
62+
Changes applied this way include anything modified in `nix/systemModules/` between the old and new SHA: new or removed systemd services, `environment.etc` entries, packages under `/run/system-manager/sw/`, etc.
2263

2364
## Nix configuration walkthrough
2465

2566
### Flake input
2667

27-
The system-manager flake input is declared in `flake.nix` (lines 34-35), pinned to the upstream repository with nixpkgs following the main input:
68+
The system-manager flake input is declared in `flake.nix`, pinned to the upstream repository with nixpkgs following the main input:
2869

2970
```nix
3071
system-manager.inputs.nixpkgs.follows = "nixpkgs";
3172
system-manager.url = "github:numtide/system-manager";
3273
```
3374

75+
The `system-manager` binary is also re-exported as a package in `nix/packages/default.nix` (Linux only), making it available as `nix profile add .#system-manager` or via the remote flake URL.
76+
3477
The flake outputs import both the module registry and the system configurations:
3578

3679
```nix
@@ -60,12 +103,12 @@ mkSystemConfig = system: {
60103
```
61104

62105
The `mkModules` function returns the list of modules to enable.
63-
Currently it enables the nginx service and sets the host platform:
106+
Currently it includes the genesis placeholder module and sets the host platform:
64107

65108
```nix
66109
mkModules = system: [
110+
self.systemModules.genesis
67111
({
68-
services.nginx.enable = true;
69112
nixpkgs.hostPlatform = system;
70113
})
71114
];
@@ -83,14 +126,21 @@ It is a flake-parts module that exports individual system modules under `flake.s
83126
imports = [ ./tests ];
84127
flake = {
85128
systemModules = {
86-
nginx = flake-parts-lib.importApply ./nginx.nix { inherit withSystem self; };
129+
genesis = {
130+
#this file is just a placeholder to bootstrap
131+
#the system manager, it will be replaced by real configurations
132+
environment.etc."system-manager-genesis" = {
133+
text = "";
134+
user = "root";
135+
group = "root";
136+
mode = "0644";
137+
};
138+
};
87139
};
88140
};
89141
}
90142
```
91143

92-
Each module is loaded with `flake-parts-lib.importApply`, which passes `withSystem` and `self` as arguments to the module file.
93-
94144
## Adding a new system module
95145

96146
To add a new system module:
@@ -134,7 +184,6 @@ To add a new system module:
134184
mkModules = system: [
135185
self.systemModules.my-service
136186
({
137-
services.nginx.enable = true;
138187
supabase.services.my-service.enable = true;
139188
nixpkgs.hostPlatform = system;
140189
})
@@ -167,13 +216,16 @@ check-system-manager =
167216
machine.activate()
168217
machine.wait_for_unit("system-manager.target")
169218
170-
with subtest("Verify nginx service"):
171-
assert machine.service("nginx").is_running, "nginx should be running"
219+
with subtest("Verify genesis file"):
220+
assert machine.file("/etc/system-manager-genesis").exists, "/etc/system-manager-genesis should exist"
221+
assert machine.file("/etc/system-manager-genesis").mode == 0o644, "/etc/system-manager-genesis should have mode 0644"
222+
assert machine.file("/etc/system-manager-genesis").user == "root", "/etc/system-manager-genesis should be owned by root"
223+
assert machine.file("/etc/system-manager-genesis").group == "root", "/etc/system-manager-genesis should be owned by root"
172224
'';
173225
};
174226
```
175227

176-
The test script starts the container, waits for systemd to reach `multi-user.target`, activates the system-manager configuration, then verifies that managed services are running.
228+
The test script starts the container, waits for systemd to reach `multi-user.target`, activates the system-manager configuration, then verifies that managed services and files are present.
177229
When adding a new module, extend the `testScript` with an additional `subtest` block that asserts the new service is running.
178230

179231
### Running tests locally
@@ -217,7 +269,7 @@ The `check-system-manager` derivation is part of the flake's `checks` output, so
217269

218270
## Runtime effects
219271

220-
After `system-manager switch` runs, managed software is available under `/run/system-manager/sw/`.
272+
After `system-manager activate` runs, managed software is available under `/run/system-manager/sw/`.
221273
This affects paths throughout the system.
222274
For example, the audit baseline `audit-specs/baselines/ami-build/user.yml` references these paths for user shells:
223275

0 commit comments

Comments
 (0)