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 return stream(io, machine, gpa, &.{ "sudo", "systemctl", verb, name });280}281282pub fn serviceLogs(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, name: []const u8, last: u32, follow: bool) !u8 {283 const n_str = try std.fmt.allocPrint(gpa, "{d}", .{last});284 defer gpa.free(n_str);285 if (follow) {286 return stream(io, machine, gpa, &.{ "journalctl", "-u", name, "-f" });287 }288 return stream(io, machine, gpa, &.{ "journalctl", "-u", name, "-n", n_str, "--no-pager" });289}290291292293pub fn profileInstall(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, attr: []const u8) !u8 {294 const pkg_ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{attr});295 defer gpa.free(pkg_ref);296 return stream(io, machine, gpa, &.{ "nix", "profile", "install", pkg_ref });297}298299pub fn profileRemove(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, attr: []const u8) !u8 {300 const pkg_ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{attr});301 defer gpa.free(pkg_ref);302 return stream(io, machine, gpa, &.{ "nix", "profile", "remove", pkg_ref });303}304305pub fn profileList(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {306 const r = try capture(gpa, io, machine, &.{ "nix", "profile", "list" });307 defer gpa.free(r.stderr);308 if (r.exit_code != 0) return error.ProcessFailed;309 return r.stdout;310}311312pub fn profileUpgrade(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {313 return stream(io, machine, gpa, &.{ "nix", "profile", "upgrade", ".*" });314}315316317318pub fn storeGc(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {319 return stream(io, machine, gpa, &.{ "sudo", "nix-collect-garbage" });320}321322pub fn storeVerify(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {323 return stream(io, machine, gpa, &.{ "nix", "store", "verify", "--all" });324}325326pub fn storeRepair(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {327 return stream(io, machine, gpa, &.{ "sudo", "nix", "store", "repair", "--all" });328}329330pub fn storeInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) !types.StoreInfo {331 const pi = try capture(gpa, io, machine, &.{ "nix", "path-info", "-S", "--all", "--json" });332 defer gpa.free(pi.stdout);333 defer gpa.free(pi.stderr);334335 var live: u64 = 0;336 if (pi.exit_code == 0) {337 live = countJsonPaths(pi.stdout);338 }339340 const du = try capture(gpa, io, machine, &.{ "du", "-sh", "/nix/store" });341 defer gpa.free(du.stderr);342 const total = blk: {343 const line = std.mem.trim(u8, du.stdout, " \t\n\r");344 const tab = std.mem.indexOfScalar(u8, line, '\t') orelse break :blk "?";345 break :blk line[0..tab];346 };347 const total_owned = try gpa.dupe(u8, total);348 gpa.free(du.stdout);349350 return .{351 .total_size = total_owned,352 .live_paths = live,353 .reclaimable_paths = 0,354 };355}356357fn countJsonPaths(json_str: []const u8) u64 {358 359 var count: u64 = 0;360 var i: usize = 0;361 while (i < json_str.len) : (i += 1) {362 if (json_str[i] == '"') count += 1;363 }364 return count / 2; 365}366367pub fn storePath(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, attr: []const u8) ![]u8 {368 const expr = try std.fmt.allocPrint(gpa, ".#{s}.outPath", .{attr});369 defer gpa.free(expr);370 const r = try capture(gpa, io, machine, &.{ "nix", "eval", "--raw", expr });371 defer gpa.free(r.stderr);372 if (r.exit_code != 0) {373 gpa.free(r.stdout);374 return error.ProcessFailed;375 }376 return r.stdout;377}378379380381pub fn flakeUpdate(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, input: ?[]const u8) !u8 {382 if (input) |inp| {383 return stream(io, machine, gpa, &.{ "nix", "flake", "update", inp });384 }385 return stream(io, machine, gpa, &.{ "nix", "flake", "update" });386}387388pub fn flakeCheck(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {389 return stream(io, machine, gpa, &.{ "nix", "flake", "check" });390}391392pub fn flakeInit(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {393 return stream(io, machine, gpa, &.{ "nix", "flake", "init" });394}395396pub fn flakeLock(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {397 return stream(io, machine, gpa, &.{ "nix", "flake", "lock" });398}399400pub fn flakeClone(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, url: []const u8) !u8 {401 return stream(io, machine, gpa, &.{ "nix", "flake", "clone", url });402}403404pub fn flakeShow(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {405 const r = try capture(gpa, io, machine, &.{ "nix", "flake", "show", "--json" });406 defer gpa.free(r.stderr);407 if (r.exit_code != 0) {408 gpa.free(r.stdout);409 return error.ProcessFailed;410 }411 return r.stdout;412}413414415416pub fn nixBuild(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, attr: ?[]const u8) !u8 {417 if (attr) |a| {418 const ref = try std.fmt.allocPrint(gpa, ".#{s}", .{a});419 defer gpa.free(ref);420 return stream(io, machine, gpa, &.{ "nix", "build", ref });421 }422 return stream(io, machine, gpa, &.{ "nix", "build" });423}424425pub fn nixRun(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {426 427 const is_url = std.mem.startsWith(u8, pkg, "http://") or std.mem.startsWith(u8, pkg, "https://") or std.mem.startsWith(u8, pkg, "github:");428 if (is_url) {429 return stream(io, machine, gpa, &.{ "nix", "run", pkg, "--no-write-lock-file" });430 }431 const ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{pkg});432 defer gpa.free(ref);433 return stream(io, machine, gpa, &.{ "nix", "run", ref });434}435436pub fn nixShell(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {437 const ref = try std.fmt.allocPrint(gpa, "nixpkgs#{s}", .{pkg});438 defer gpa.free(ref);439 return stream(io, machine, gpa, &.{ "nix", "shell", ref });440}441442pub fn nixDevelop(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, run_cmd: ?[]const u8) !u8 {443 if (run_cmd) |cmd| {444 return stream(io, machine, gpa, &.{ "nix", "develop", "--command", cmd });445 }446 return stream(io, machine, gpa, &.{ "nix", "develop" });447}448449pub fn nixRepl(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator) !u8 {450 return stream(io, machine, gpa, &.{ "nix", "repl", "--expr", "import <nixpkgs> {}" });451}452453454455pub fn nixpkgsFmt(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, path: []const u8, check: bool) !u8 {456 if (check) {457 return stream(io, machine, gpa, &.{ "nixpkgs-fmt", "--check", path });458 }459 return stream(io, machine, gpa, &.{ "nixpkgs-fmt", path });460}461462463464pub fn systemInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {465 const r = try capture(gpa, io, machine, &.{"nixos-version"});466 defer gpa.free(r.stderr);467 if (r.exit_code != 0) {468 gpa.free(r.stdout);469 return error.ProcessFailed;470 }471 return r.stdout;472}473474pub fn kernelInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {475 const r = try capture(gpa, io, machine, &.{ "uname", "-r" });476 defer gpa.free(r.stderr);477 if (r.exit_code != 0) {478 gpa.free(r.stdout);479 return error.ProcessFailed;480 }481 return r.stdout;482}483484pub fn uptimeInfo(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]u8 {485 const r = try capture(gpa, io, machine, &.{"uptime"});486 defer gpa.free(r.stderr);487 if (r.exit_code != 0) {488 gpa.free(r.stdout);489 return error.ProcessFailed;490 }491 return r.stdout;492}493494495496pub fn bootEntries(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.BootEntry {497 const r = try capture(gpa, io, machine, &.{ "bootctl", "list" });498 defer gpa.free(r.stderr);499 defer gpa.free(r.stdout);500 if (r.exit_code != 0) return error.ProcessFailed;501 return parseBootEntries(gpa, r.stdout);502}503504fn parseBootEntries(gpa: std.mem.Allocator, output_str: []const u8) ![]types.BootEntry {505 var entries: std.ArrayList(types.BootEntry) = .empty;506 var lines = std.mem.splitScalar(u8, output_str, '\n');507 while (lines.next()) |line| {508 const trimmed = std.mem.trim(u8, line, " \t\r");509 if (std.mem.startsWith(u8, trimmed, "title:")) {510 const title = std.mem.trim(u8, trimmed[6..], " \t");511 const current = std.mem.indexOf(u8, title, "(current)") != null;512 try entries.append(gpa, .{513 .title = try gpa.dupe(u8, title),514 .current = current,515 });516 }517 }518 return entries.toOwnedSlice(gpa);519}520521522523pub fn pkgWhy(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {524 return stream(io, machine, gpa, &.{ "nix", "why-depends", ".#", pkg });525}526527pub fn pkgDeps(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {528 return stream(io, machine, gpa, &.{ "nix-store", "--query", "--requisites", pkg });529}530531pub fn pkgSize(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, pkg: []const u8) ![]u8 {532 const r = try capture(gpa, io, machine, &.{ "nix", "path-info", "-s", pkg });533 defer gpa.free(r.stderr);534 if (r.exit_code != 0) {535 gpa.free(r.stdout);536 return error.ProcessFailed;537 }538 return r.stdout;539}540541pub fn pkgClosure(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, pkg: []const u8) !u8 {542 return stream(io, machine, gpa, &.{ "nix", "path-info", "-rS", pkg });543}544545546547pub fn nixHash(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, path: []const u8) ![]u8 {548 const r = try capture(gpa, io, machine, &.{ "nix", "hash", "path", path });549 defer gpa.free(r.stderr);550 if (r.exit_code != 0) {551 gpa.free(r.stdout);552 return error.ProcessFailed;553 }554 return r.stdout;555}556557pub fn nixFetch(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine, url: []const u8) ![]u8 {558 const r = try capture(gpa, io, machine, &.{ "nix-prefetch-url", url });559 defer gpa.free(r.stderr);560 if (r.exit_code != 0) {561 gpa.free(r.stdout);562 return error.ProcessFailed;563 }564 return r.stdout;565}566567568569pub fn editFlakeLock(_: std.mem.Allocator, _: []const u8, _: []const u8) !void {570 571 572}573574pub fn openEditor(io: std.Io, gpa: std.mem.Allocator, editor: []const u8, path: []const u8, line: ?u32) !u8 {575 if (line) |l| {576 const line_arg = try std.fmt.allocPrint(gpa, "+{d}", .{l});577 defer gpa.free(line_arg);578 return stream(io, &types.Machine{}, gpa, &.{ editor, path, line_arg });579 }580 return stream(io, &types.Machine{}, gpa, &.{ editor, path });581}582583pub fn diffClosures(io: std.Io, machine: *const types.Machine, gpa: std.mem.Allocator, a: u32, b: u32) !u8 {584 const a_str = try std.fmt.allocPrint(gpa, "/nix/var/nix/profiles/system-{d}-link", .{a});585 defer gpa.free(a_str);586 const b_str = try std.fmt.allocPrint(gpa, "/nix/var/nix/profiles/system-{d}-link", .{b});587 defer gpa.free(b_str);588 return stream(io, machine, gpa, &.{ "nix", "store", "diff-closures", a_str, b_str });589}590591592593pub fn runDoctorChecks(gpa: std.mem.Allocator, io: std.Io, machine: *const types.Machine) ![]types.DoctorCheck {594 var checks: std.ArrayList(types.DoctorCheck) = .empty;595596 597 {598 const r = try capture(gpa, io, machine, &.{ "systemctl", "is-active", "nix-daemon" });599 defer gpa.free(r.stdout);600 defer gpa.free(r.stderr);601 const active = std.mem.indexOf(u8, r.stdout, "active") != null;602 try checks.append(gpa, .{ .name = "nix daemon", .status = if (active) .ok else .fail });603 }604605 606 {607 const r = try capture(gpa, io, machine, &.{ "sudo", "nixos-rebuild", "build", "--dry-run" });608 defer gpa.free(r.stdout);609 defer gpa.free(r.stderr);610 try checks.append(gpa, .{ .name = "config syntax", .status = if (r.exit_code == 0) .ok else .fail });611 }612613 614 {615 const r = try capture(gpa, io, machine, &.{ "nix-channel", "--list" });616 defer gpa.free(r.stdout);617 defer gpa.free(r.stderr);618 const has_channel = r.exit_code == 0 and r.stdout.len > 0;619 try checks.append(gpa, .{ .name = "channel", .status = if (has_channel) .ok else .warn });620 }621622 return checks.toOwnedSlice(gpa);623}624625626627pub fn readNinaLog(gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, last: u32) ![]u8 {628 const home = environ.get("HOME") orelse return error.FileNotFound;629 const path = try std.fmt.allocPrint(gpa, "{s}/.nina.log", .{home});630 defer gpa.free(path);631 _ = last;632 return std.Io.Dir.readFileAlloc(.cwd(), io, path, gpa, .unlimited) catch return error.FileNotFound;633}634635pub fn appendNinaLog(io: std.Io, environ: *const std.process.Environ.Map, entry: []const u8) void {636 const home = environ.get("HOME") orelse return;637 var path_buf: [512]u8 = undefined;638 const path = std.fmt.bufPrint(&path_buf, "{s}/.nina.log", .{home}) catch return;639 const file = std.Io.Dir.openFile(.cwd(), io, path, .{ .mode = .read_write }) catch640 (std.Io.Dir.createFile(.cwd(), io, path, .{}) catch return);641 defer file.close(io);642 const size = file.length(io) catch 0;643 file.writePositionalAll(io, entry, size) catch {};644}