123const std = @import("std");4const builtin = @import("builtin");5const types = @import("types.zig");6const errors = @import("errors.zig");7const output = @import("output.zig");891011const SYSTEM_PROFILE = "/nix/var/nix/profiles/system";1213pub const RunResult = struct {14 stdout: []u8,15 stderr: []u8,16 exit_code: u8,17};181920pub fn capture(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, cmd: []const []const u8) !RunResult {21 const argv = try buildArgv(gpa, machine, cmd);22 defer if (!machine.local) gpa.free(argv);2324 const result = std.process.run(gpa, io, .{ .argv = argv }) catch |e| {25 errors.error_info.setMessage("process failed: {s}", .{cmd[0]});26 return e;27 };2829 return .{30 .stdout = result.stdout,31 .stderr = result.stderr,32 .exit_code = switch (result.term) {33 .exited => |c| c,34 else => 1,35 },36 };37}383940pub fn stream(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, cmd: []const []const u8) !u8 {41 const argv = try buildArgv(gpa, machine, cmd);42 defer if (!machine.local) gpa.free(argv);4344 var child = std.process.spawn(io, .{45 .argv = argv,46 .stdin = .inherit,47 .stdout = .inherit,48 .stderr = .inherit,49 }) catch |e| {50 errors.error_info.setMessage("failed to spawn: {s}", .{cmd[0]});51 return e;52 };5354 const term = child.wait(io) catch |e| {55 errors.error_info.setMessage("process wait failed", .{});56 return e;57 };5859 return switch (term) {60 .exited => |c| c,61 else => 1,62 };63}646566pub fn captureLocal(gpa: std.mem.Allocator, io: std.Io, cmd: []const []const u8) ![]u8 {67 const result = std.process.run(gpa, io, .{ .argv = cmd }) catch return error.ProcessFailed;68 defer gpa.free(result.stderr);69 switch (result.term) {70 .exited => |c| if (c != 0) {71 gpa.free(result.stdout);72 return error.ProcessFailed;73 },74 else => {75 gpa.free(result.stdout);76 return error.ProcessFailed;77 },78 }79 return result.stdout;80}8182fn buildArgv(gpa: std.mem.Allocator, machine: *const types.Machine, cmd: []const []const u8) ![]const []const u8 {83 if (machine.local) return cmd;8485 const host = machine.host orelse {86 errors.error_info.setMessage("machine '{s}' has no host configured", .{machine.name});87 return error.SshFailed;88 };8990 var ssh: std.ArrayList([]const u8) = .empty;91 try ssh.append(gpa, "ssh");92 try ssh.append(gpa, "-o");93 try ssh.append(gpa, "BatchMode=yes");94 try ssh.append(gpa, "-o");95 try ssh.append(gpa, "StrictHostKeyChecking=accept-new");96 if (machine.ssh_key) |key| {97 try ssh.append(gpa, "-i");98 try ssh.append(gpa, key);99 }100 const addr = if (machine.user) |u|101 try std.fmt.allocPrint(gpa, "{s}@{s}", .{ u, host })102 else103 try gpa.dupe(u8, host);104 try ssh.append(gpa, addr);105 for (cmd) |arg| try ssh.append(gpa, arg);106 return ssh.toOwnedSlice(gpa);107}108109110111pub fn nixosRebuildSwitch(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {112 const cmd = &.{ "sudo", "nixos-rebuild", "switch" };113 output.printSubstep("nixos-rebuild switch", .{});114 return stream(io, machine, gpa, cmd);115}116117pub fn nixosRebuildDryActivate(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {118 return stream(io, machine, gpa, &.{ "sudo", "nixos-rebuild", "dry-activate" });119}120121pub fn nixosRebuildBuild(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {122 return stream(io, machine, gpa, &.{ "sudo", "nixos-rebuild", "build" });123}124125126127pub fn getGenerations(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.GenerationInfo {128 const r = try capture(gpa, io, machine, &.{ "nix-env", "-p", SYSTEM_PROFILE, "--list-generations" });129 defer gpa.free(r.stderr);130 defer gpa.free(r.stdout);131 if (r.exit_code != 0) return error.ProcessFailed;132 return parseGenerations(gpa, r.stdout);133}134135fn parseGenerations(gpa: std.mem.Allocator, output_str: []const u8) ![]types.GenerationInfo {136 var gens: std.ArrayList(types.GenerationInfo) = .empty;137 var lines = std.mem.splitScalar(u8, output_str, '\n');138 while (lines.next()) |line| {139 const trimmed = std.mem.trim(u8, line, " \t");140 if (trimmed.len == 0) continue;141 var parts = std.mem.tokenizeAny(u8, trimmed, " \t");142 const num_str = parts.next() orelse continue;143 const date_str = parts.next() orelse continue;144 const time_str = parts.next() orelse continue;145 const rest = parts.rest();146 const current = std.mem.indexOf(u8, rest, "current") != null;147 const num = std.fmt.parseInt(u32, num_str, 10) catch continue;148 try gens.append(gpa, .{149 .number = num,150 .date = try gpa.dupe(u8, date_str),151 .time = try gpa.dupe(u8, time_str),152 .current = current,153 });154 }155 return gens.toOwnedSlice(gpa);156}157158pub fn getCurrentGeneration(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) !u32 {159 const gens = try getGenerations(gpa, io, machine);160 defer gpa.free(gens);161 for (gens) |g| {162 if (g.current) return g.number;163 }164 return 0;165}166167pub fn switchGeneration(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, n: u32) !u8 {168 169 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 });173}174175pub fn rollback(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {176 return stream(io, machine, gpa, &.{ "sudo", "nixos-rebuild", "switch", "--rollback" });177}178179pub fn deleteGeneration(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, n: u32) !u8 {180 const n_str = try std.fmt.allocPrint(gpa, "{d}", .{n});181 defer gpa.free(n_str);182 return stream(io, machine, gpa, &.{ "sudo", "nix-env", "-p", SYSTEM_PROFILE, "--delete-generations", n_str });183}184185pub fn deleteOldGenerations(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {186 return stream(io, machine, gpa, &.{ "sudo", "nix-env", "-p", SYSTEM_PROFILE, "--delete-generations", "old" });187}188189190191pub fn collectGarbage(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, delete_old: bool, keep: u32) !u8 {192 if (delete_old) {193 return stream(io, machine, gpa, &.{ "sudo", "nix-collect-garbage", "-d" });194 }195 196 197 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 });201}202203204205pub fn channelList(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.ChannelInfo {206 const r = try capture(gpa, io, machine, &.{ "nix-channel", "--list" });207 defer gpa.free(r.stderr);208 defer gpa.free(r.stdout);209 if (r.exit_code != 0) return error.ProcessFailed;210 return parseChannels(gpa, r.stdout);211}212213fn parseChannels(gpa: std.mem.Allocator, output_str: []const u8) ![]types.ChannelInfo {214 var channels: std.ArrayList(types.ChannelInfo) = .empty;215 var lines = std.mem.splitScalar(u8, output_str, '\n');216 while (lines.next()) |line| {217 const trimmed = std.mem.trim(u8, line, " \t\r");218 if (trimmed.len == 0) continue;219 const space = std.mem.indexOfScalar(u8, trimmed, ' ') orelse continue;220 try channels.append(gpa, .{221 .name = try gpa.dupe(u8, trimmed[0..space]),222 .url = try gpa.dupe(u8, std.mem.trim(u8, trimmed[space + 1 ..], " \t")),223 });224 }225 return channels.toOwnedSlice(gpa);226}227228pub fn channelUpdate(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {229 return stream(io, machine, gpa, &.{ "sudo", "nix-channel", "--update" });230}231232pub fn channelAdd(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, url: []const u8, name: []const u8) !u8 {233 return stream(io, machine, gpa, &.{ "sudo", "nix-channel", "--add", url, name });234}235236pub fn channelRemove(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, name: []const u8) !u8 {237 return stream(io, machine, gpa, &.{ "sudo", "nix-channel", "--remove", name });238}239240241242pub fn serviceList(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.ServiceInfo {243 const r = try capture(gpa, io, machine, &.{ "systemctl", "list-units", "--type=service", "--no-legend", "--no-pager" });244 defer gpa.free(r.stderr);245 defer gpa.free(r.stdout);246 if (r.exit_code != 0) return error.ProcessFailed;247 return parseServices(gpa, r.stdout);248}249250fn parseServices(gpa: std.mem.Allocator, output_str: []const u8) ![]types.ServiceInfo {251 var svcs: std.ArrayList(types.ServiceInfo) = .empty;252 var lines = std.mem.splitScalar(u8, output_str, '\n');253 while (lines.next()) |line| {254 const trimmed = std.mem.trim(u8, line, " \t\r");255 if (trimmed.len == 0 or trimmed[0] == 0xE2) continue; 256 var parts = std.mem.tokenizeAny(u8, trimmed, " \t");257 const name = parts.next() orelse continue;258 _ = parts.next(); 259 const active_str = parts.next() orelse continue;260 const sub_str = parts.next() orelse "unknown";261262 const state: types.ServiceState = if (std.mem.eql(u8, active_str, "active"))263 (if (std.mem.eql(u8, sub_str, "running") or std.mem.eql(u8, sub_str, "exited")) .active else .active)264 else if (std.mem.eql(u8, active_str, "failed"))265 .failed266 else267 .inactive;268269 try svcs.append(gpa, .{270 .name = try gpa.dupe(u8, name),271 .state = state,272 .uptime = null,273 });274 }275 return svcs.toOwnedSlice(gpa);276}277278pub fn serviceCtl(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, verb: []const u8, name: []const u8) !u8 {279 280 return stream(io, machine, gpa, &.{ "sudo", "systemctl", "--no-pager", verb, name });281}282283pub fn serviceLogs(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, name: []const u8, last: u32, follow: bool) !u8 {284 const n_str = try std.fmt.allocPrint(gpa, "{d}", .{last});285 defer gpa.free(n_str);286 if (follow) {287 return stream(io, machine, gpa, &.{ "journalctl", "-u", name, "-f" });288 }289 return stream(io, machine, gpa, &.{ "journalctl", "-u", name, "-n", n_str, "--no-pager" });290}291292293294pub fn profileInstall(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, attr: []const u8) !u8 {295 const pkg_ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{attr});296 defer gpa.free(pkg_ref);297 return stream(io, machine, gpa, &.{ "nix", "profile", "install", pkg_ref });298}299300pub fn profileRemove(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, attr: []const u8) !u8 {301 const pkg_ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{attr});302 defer gpa.free(pkg_ref);303 return stream(io, machine, gpa, &.{ "nix", "profile", "remove", pkg_ref });304}305306pub fn profileList(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {307 const r = try capture(gpa, io, machine, &.{ "nix", "profile", "list" });308 defer gpa.free(r.stderr);309 if (r.exit_code != 0) return error.ProcessFailed;310 return r.stdout;311}312313pub fn profileUpgrade(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {314 return stream(io, machine, gpa, &.{ "nix", "profile", "upgrade", ".*" });315}316317318319pub fn storeGc(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {320 return stream(io, machine, gpa, &.{ "sudo", "nix-collect-garbage" });321}322323pub fn storeVerify(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {324 return stream(io, machine, gpa, &.{ "nix", "store", "verify", "--all" });325}326327pub fn storeRepair(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {328 return stream(io, machine, gpa, &.{ "sudo", "nix", "store", "repair", "--all" });329}330331pub fn storeInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) !types.StoreInfo {332 const pi = try capture(gpa, io, machine, &.{ "nix", "path-info", "-S", "--all", "--json" });333 defer gpa.free(pi.stdout);334 defer gpa.free(pi.stderr);335336 var live: u64 = 0;337 if (pi.exit_code == 0) {338 live = countJsonPaths(pi.stdout);339 }340341 const du = try capture(gpa, io, machine, &.{ "du", "-sh", "/nix/store" });342 defer gpa.free(du.stderr);343 const total = blk: {344 const line = std.mem.trim(u8, du.stdout, " \t\n\r");345 const tab = std.mem.indexOfScalar(u8, line, '\t') orelse break :blk "?";346 break :blk line[0..tab];347 };348 const total_owned = try gpa.dupe(u8, total);349 gpa.free(du.stdout);350351 return .{352 .total_size = total_owned,353 .live_paths = live,354 .reclaimable_paths = 0,355 };356}357358fn countJsonPaths(json_str: []const u8) u64 {359 360 var count: u64 = 0;361 var i: usize = 0;362 while (i < json_str.len) : (i += 1) {363 if (json_str[i] == '"') count += 1;364 }365 return count / 2; 366}367368pub fn storePath(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, attr: []const u8) ![]u8 {369 const expr = try std.fmt.allocPrint(gpa, ".#{s}.outPath", .{attr});370 defer gpa.free(expr);371 const r = try capture(gpa, io, machine, &.{ "nix", "eval", "--raw", expr });372 defer gpa.free(r.stderr);373 if (r.exit_code != 0) {374 gpa.free(r.stdout);375 return error.ProcessFailed;376 }377 return r.stdout;378}379380381382pub fn flakeUpdate(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, input: ?[]const u8) !u8 {383 if (input) |inp| {384 return stream(io, machine, gpa, &.{ "nix", "flake", "update", inp });385 }386 return stream(io, machine, gpa, &.{ "nix", "flake", "update" });387}388389pub fn flakeCheck(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {390 return stream(io, machine, gpa, &.{ "nix", "flake", "check" });391}392393pub fn flakeInit(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {394 return stream(io, machine, gpa, &.{ "nix", "flake", "init" });395}396397pub fn flakeLock(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {398 return stream(io, machine, gpa, &.{ "nix", "flake", "lock" });399}400401pub fn flakeClone(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, url: []const u8) !u8 {402 return stream(io, machine, gpa, &.{ "nix", "flake", "clone", url });403}404405pub fn flakeShow(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {406 const r = try capture(gpa, io, machine, &.{ "nix", "flake", "show", "--json" });407 defer gpa.free(r.stderr);408 if (r.exit_code != 0) {409 gpa.free(r.stdout);410 return error.ProcessFailed;411 }412 return r.stdout;413}414415416417pub fn nixBuild(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, attr: ?[]const u8) !u8 {418 if (attr) |a| {419 const ref = try std.fmt.allocPrint(gpa, ".#{s}", .{a});420 defer gpa.free(ref);421 return stream(io, machine, gpa, &.{ "nix", "build", ref });422 }423 return stream(io, machine, gpa, &.{ "nix", "build" });424}425426pub fn nixRun(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {427 428 const is_url = std.mem.startsWith(u8, pkg, "http://") or std.mem.startsWith(u8, pkg, "https://") or std.mem.startsWith(u8, pkg, "github:");429 if (is_url) {430 return stream(io, machine, gpa, &.{ "nix", "run", pkg, "--no-write-lock-file" });431 }432 const ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{pkg});433 defer gpa.free(ref);434 return stream(io, machine, gpa, &.{ "nix", "run", ref });435}436437pub fn nixShell(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {438 const ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{pkg});439 defer gpa.free(ref);440 return stream(io, machine, gpa, &.{ "nix", "shell", ref });441}442443pub fn nixDevelop(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, run_cmd: ?[]const u8) !u8 {444 if (run_cmd) |cmd| {445 return stream(io, machine, gpa, &.{ "nix", "develop", "--command", cmd });446 }447 return stream(io, machine, gpa, &.{ "nix", "develop" });448}449450pub fn nixRepl(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {451 return stream(io, machine, gpa, &.{ "nix", "repl", "--expr", "import <nixpkgs> {}" });452}453454455456pub fn nixpkgsFmt(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, path: []const u8, check: bool) !u8 {457 if (check) {458 return stream(io, machine, gpa, &.{ "nixpkgs-fmt", "--check", path });459 }460 return stream(io, machine, gpa, &.{ "nixpkgs-fmt", path });461}462463464465pub fn systemInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {466 const r = try capture(gpa, io, machine, &.{"nixos-version"});467 defer gpa.free(r.stderr);468 if (r.exit_code != 0) {469 gpa.free(r.stdout);470 return error.ProcessFailed;471 }472 return r.stdout;473}474475pub fn kernelInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {476 const r = try capture(gpa, io, machine, &.{ "uname", "-r" });477 defer gpa.free(r.stderr);478 if (r.exit_code != 0) {479 gpa.free(r.stdout);480 return error.ProcessFailed;481 }482 return r.stdout;483}484485pub fn uptimeInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {486 const r = try capture(gpa, io, machine, &.{"uptime"});487 defer gpa.free(r.stderr);488 if (r.exit_code != 0) {489 gpa.free(r.stdout);490 return error.ProcessFailed;491 }492 return r.stdout;493}494495496497pub fn bootEntries(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.BootEntry {498 const r = try capture(gpa, io, machine, &.{ "bootctl", "list" });499 defer gpa.free(r.stderr);500 defer gpa.free(r.stdout);501 if (r.exit_code != 0) return error.ProcessFailed;502 return parseBootEntries(gpa, r.stdout);503}504505fn parseBootEntries(gpa: std.mem.Allocator, output_str: []const u8) ![]types.BootEntry {506 var entries: std.ArrayList(types.BootEntry) = .empty;507 var lines = std.mem.splitScalar(u8, output_str, '\n');508 while (lines.next()) |line| {509 const trimmed = std.mem.trim(u8, line, " \t\r");510 if (std.mem.startsWith(u8, trimmed, "title:")) {511 const title = std.mem.trim(u8, trimmed[6..], " \t");512 const current = std.mem.indexOf(u8, title, "(current)") != null;513 try entries.append(gpa, .{514 .title = try gpa.dupe(u8, title),515 .current = current,516 });517 }518 }519 return entries.toOwnedSlice(gpa);520}521522523524pub fn pkgWhy(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {525 return stream(io, machine, gpa, &.{ "nix", "why-depends", ".#", pkg });526}527528pub fn pkgDeps(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {529 return stream(io, machine, gpa, &.{ "nix-store", "--query", "--requisites", pkg });530}531532pub fn pkgSize(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, pkg: []const u8) ![]u8 {533 const r = try capture(gpa, io, machine, &.{ "nix", "path-info", "-s", pkg });534 defer gpa.free(r.stderr);535 if (r.exit_code != 0) {536 gpa.free(r.stdout);537 return error.ProcessFailed;538 }539 return r.stdout;540}541542pub fn pkgClosure(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {543 return stream(io, machine, gpa, &.{ "nix", "path-info", "-rS", pkg });544}545546547548pub fn nixHash(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, path: []const u8) ![]u8 {549 const r = try capture(gpa, io, machine, &.{ "nix", "hash", "path", path });550 defer gpa.free(r.stderr);551 if (r.exit_code != 0) {552 gpa.free(r.stdout);553 return error.ProcessFailed;554 }555 return r.stdout;556}557558pub fn nixFetch(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, url: []const u8) ![]u8 {559 const r = try capture(gpa, io, machine, &.{ "nix-prefetch-url", url });560 defer gpa.free(r.stderr);561 if (r.exit_code != 0) {562 gpa.free(r.stdout);563 return error.ProcessFailed;564 }565 return r.stdout;566}567568569570pub fn editFlakeLock(_: std.mem.Allocator, _: []const u8, _: []const u8) !void {571 572 573}574575pub fn openEditor(io: std.Io, gpa: std.mem.Allocator, editor: []const u8, path: []const u8, line: ?u32) !u8 {576 if (line) |l| {577 const line_arg = try std.fmt.allocPrint(gpa, "+{d}", .{l});578 defer gpa.free(line_arg);579 return stream(io, &types.Machine{}, gpa, &.{ editor, path, line_arg });580 }581 return stream(io, &types.Machine{}, gpa, &.{ editor, path });582}583584pub fn diffClosures(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, a: u32, b: u32) !u8 {585 const a_str = try std.fmt.allocPrint(gpa, "/nix/var/nix/profiles/system-{d}-link", .{a});586 defer gpa.free(a_str);587 const b_str = try std.fmt.allocPrint(gpa, "/nix/var/nix/profiles/system-{d}-link", .{b});588 defer gpa.free(b_str);589 return stream(io, machine, gpa, &.{ "nix", "store", "diff-closures", a_str, b_str });590}591592593594pub fn runDoctorChecks(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.DoctorCheck {595 var checks: std.ArrayList(types.DoctorCheck) = .empty;596597 598 {599 const r = try capture(gpa, io, machine, &.{ "systemctl", "is-active", "nix-daemon" });600 defer gpa.free(r.stdout);601 defer gpa.free(r.stderr);602 const active = std.mem.indexOf(u8, r.stdout, "active") != null;603 try checks.append(gpa, .{ .name = "nix daemon", .status = if (active) .ok else .fail });604 }605606 607 {608 const r = try capture(gpa, io, machine, &.{ "sudo", "nixos-rebuild", "dry-build" });609 defer gpa.free(r.stdout);610 defer gpa.free(r.stderr);611 try checks.append(gpa, .{ .name = "config syntax", .status = if (r.exit_code == 0) .ok else .fail });612 }613614 615 {616 const r = try capture(gpa, io, machine, &.{ "nix-channel", "--list" });617 defer gpa.free(r.stdout);618 defer gpa.free(r.stderr);619 const has_channel = r.exit_code == 0 and r.stdout.len > 0;620 try checks.append(gpa, .{ .name = "channel", .status = if (has_channel) .ok else .warn });621 }622623 return checks.toOwnedSlice(gpa);624}625626627628pub fn readNinaLog(gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, last: u32) ![]u8 {629 const home = environ.get("HOME") orelse return error.FileNotFound;630 const path = try std.fmt.allocPrint(gpa, "{s}/.nina.log", .{home});631 defer gpa.free(path);632 _ = last;633 return std.Io.Dir.readFileAlloc(.cwd(), io, path, gpa, .unlimited) catch return error.FileNotFound;634}635636pub fn appendNinaLog(io: std.Io, environ: *const std.process.Environ.Map, entry: []const u8) void {637 const home = environ.get("HOME") orelse return;638 var path_buf: [512]u8 = undefined;639 const path = std.fmt.bufPrint(&path_buf, "{s}/.nina.log", .{home}) catch return;640 const file = std.Io.Dir.openFile(.cwd(), io, path, .{ .mode = .read_write }) catch641 (std.Io.Dir.createFile(.cwd(), io, path, .{}) catch return);642 defer file.close(io);643 const size = file.length(io) catch 0;644 file.writePositionalAll(io, entry, size) catch {};645}