7ae2557619ee

fix generation profile bug (-p system, sudo, activate on go) and clean (delete-generations +N) in exec.zig; v3.0.1

time
2026-05-29 16:33 UTC · 4 months ago
machine
koh2
parent
d0e45535ef46
3 files build.zig.zon+1 −1src/commands.zig+1 −1src/exec.zig+19 −10
build.zig.zon+1 −1
@@ -9,7 +9,7 @@99     .name = .nina,1010     // This is a [Semantic Version](https://semver.org/).1111     // In a future version of Zig it will be used for package deduplication.12-    .version = "3.0.0",12+    .version = "3.0.1",1313     // Together with name, this represents a globally unique package1414     // identifier. This field is generated by the Zig toolchain when the1515     // package is first created, and then *never changes*. This allows

view file

src/commands.zig+1 −1
@@ -132,7 +132,7 @@132132     output.cleanStart(ctx.machine.name, ctx.cfg.generations, @intCast(gens.len));133133     if (ctx.cfg.confirm and !output.confirm("removing — cannot be undone")) return;134134     const all = ctx.all or std.mem.eql(u8, ctx.sub, "all");135-    _ = try exec.collectGarbage(ctx.io, &ctx.machine, ctx.gpa, all);135+    _ = try exec.collectGarbage(ctx.io, &ctx.machine, ctx.gpa, all, ctx.cfg.generations);136136     output.cleanDone("?");137137 }138138 

view file

src/exec.zig+19 −10
@@ -6,6 +6,10 @@66 const errors = @import("errors.zig");77 const output = @import("output.zig");88 9+// nixos-rebuild manages this profile; generation commands must target it10+// explicitly. Bare nix-env defaults to the per-user profile, which is empty.11+const SYSTEM_PROFILE = "/nix/var/nix/profiles/system";12+913 pub const RunResult = struct {1014     stdout: []u8,1115     stderr: []u8,@@ -121,7 +125,7 @@121125 // --- Generations ---122126 123127 pub fn getGenerations(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.GenerationInfo {124-    const r = try capture(gpa, io, machine, &.{ "nix-env", "--list-generations" });128+    const r = try capture(gpa, io, machine, &.{ "nix-env", "-p", SYSTEM_PROFILE, "--list-generations" });125129     defer gpa.free(r.stderr);126130     defer gpa.free(r.stdout);127131     if (r.exit_code != 0) return error.ProcessFailed;@@ -161,9 +165,11 @@161165 }162166 163167 pub fn switchGeneration(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, n: u32) !u8 {164-    const n_str = try std.fmt.allocPrint(gpa, "{d}", .{n});165-    defer gpa.free(n_str);166-    return stream(io, machine, gpa, &.{ "nix-env", "--switch-generation", n_str });168+    // Switch the system profile to generation n, then activate it (bootloader +169+    // services). Without switch-to-configuration the change isn't applied.170+    const cmd = try std.fmt.allocPrint(gpa, "nix-env -p {s} --switch-generation {d} && {s}/bin/switch-to-configuration switch", .{ SYSTEM_PROFILE, n, SYSTEM_PROFILE });171+    defer gpa.free(cmd);172+    return stream(io, machine, gpa, &.{ "sudo", "sh", "-c", cmd });167173 }168174 169175 pub fn rollback(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {@@ -173,22 +179,25 @@173179 pub fn deleteGeneration(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, n: u32) !u8 {174180     const n_str = try std.fmt.allocPrint(gpa, "{d}", .{n});175181     defer gpa.free(n_str);176-    return stream(io, machine, gpa, &.{ "nix-env", "--delete-generations", n_str });182+    return stream(io, machine, gpa, &.{ "sudo", "nix-env", "-p", SYSTEM_PROFILE, "--delete-generations", n_str });177183 }178184 179185 pub fn deleteOldGenerations(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {180-    return stream(io, machine, gpa, &.{ "nix-env", "--delete-generations", "old" });186+    return stream(io, machine, gpa, &.{ "sudo", "nix-env", "-p", SYSTEM_PROFILE, "--delete-generations", "old" });181187 }182188 183189 // --- Garbage collection ---184190 185-pub fn collectGarbage(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, delete_old: bool) !u8 {191+pub fn collectGarbage(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, delete_old: bool, keep: u32) !u8 {186192     if (delete_old) {187193         return stream(io, machine, gpa, &.{ "sudo", "nix-collect-garbage", "-d" });188194     }189-    const keep_str = try std.fmt.allocPrint(gpa, "{d}", .{5});190-    defer gpa.free(keep_str);191-    return stream(io, machine, gpa, &.{ "sudo", "nix-collect-garbage", "--delete-older-than", keep_str });195+    // Keep the `keep` most recent system generations (+N deletes all but N),196+    // then collect now-unreferenced store paths. `--delete-older-than` wants a197+    // duration (e.g. 14d), not a generation count.198+    const cmd = try std.fmt.allocPrint(gpa, "nix-env -p {s} --delete-generations +{d} && nix-collect-garbage", .{ SYSTEM_PROFILE, keep });199+    defer gpa.free(cmd);200+    return stream(io, machine, gpa, &.{ "sudo", "sh", "-c", cmd });192201 }193202 194203 // --- Channels ---

view file