commands.zig

35.6 KB · raw

1// commands.zig — all command handlers2// Orchestrates exec/api/output. Zero direct stdout writes.3const std = @import("std");4const builtin = @import("builtin");5const types = @import("types.zig");6const errors = @import("errors.zig");7const config = @import("config.zig");8const output = @import("output.zig");9const exec = @import("exec.zig");10const api = @import("api.zig");11const search_tui = @import("search.zig");12const version = @import("version.zig");1314const UPDATE_URL = "https://kepr.uk/api/nina/releases/latest";1516pub const Ctx = struct {17    gpa: std.mem.Allocator,18    io: std.Io,19    environ: *const std.process.Environ.Map,20    machine: types.Machine,21    cfg: types.NinaConfig,22    args: []const []const u8,   // positional args after subcommand23    sub: []const u8,            // subcommand (e.g. "list" in "service list")24    dry: bool,25    check: bool,26    no_apply: bool,27    all: bool,28    last: u32,29    json: bool,30};3132pub fn apply(ctx: Ctx) !void {33    if (ctx.dry) {34        output.applyDry(ctx.machine.name);35        const code = try exec.nixosRebuildDryActivate(ctx.io, &ctx.machine, ctx.gpa);36        if (code != 0) return error.BuildFailed;37        output.applyDryDone();38        return;39    }40    if (ctx.check) {41        return check(ctx);42    }4344    output.applyStart(ctx.machine.name);45    const start = std.Io.Clock.awake.now(ctx.io);4647    const code = exec.nixosRebuildSwitch(ctx.io, &ctx.machine, ctx.gpa) catch |e| {48        exec.markApplyFailed(ctx.io, ctx.environ);49        output.applyFailed("");50        return e;51    };5253    if (code != 0) {54        exec.markApplyFailed(ctx.io, ctx.environ);55        output.applyFailed("");56        return error.BuildFailed;57    }5859    const elapsed_ns = @max(0, start.untilNow(ctx.io, .awake).toNanoseconds());60    const elapsed: u64 = @intCast(@divFloor(elapsed_ns, 1_000_000));61    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine) catch 0;62    const after_failure = exec.takeApplyFailed(ctx.io, ctx.environ);63    output.applyDone(gen, elapsed, after_failure);64    logAction(ctx, "apply", gen);65}6667pub fn check(ctx: Ctx) !void {68    output.checkStart(ctx.machine.name);69    const code = try exec.nixosRebuildBuild(ctx.io, &ctx.machine, ctx.gpa);70    if (code != 0) {71        output.checkFailed("");72        return error.BuildFailed;73    }74    output.checkOk();75}7677pub fn back(ctx: Ctx) !void {78    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);79    defer ctx.gpa.free(gens);8081    var current: u32 = 0;82    for (gens) |g| if (g.current) { current = g.number; };83    const prev = if (current > 1) current - 1 else current;8485    output.backStart(ctx.machine.name, current, prev);86    if (ctx.cfg.confirm and !output.confirm("")) return;8788    const code = try exec.rollback(ctx.io, &ctx.machine, ctx.gpa);89    if (code != 0) return error.BuildFailed;90    output.backDone(prev);91}9293pub fn go(ctx: Ctx) !void {94    if (ctx.args.len == 0) return error.Internal;95    const target = std.fmt.parseInt(u32, ctx.args[0], 10) catch return error.Internal;96    const current = try exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine);9798    output.goStart(ctx.machine.name, current, target);99    if (ctx.cfg.confirm and !output.confirm("")) return;100101    const code = try exec.switchGeneration(ctx.io, &ctx.machine, ctx.gpa, target);102    if (code != 0) return error.ProcessFailed;103    output.generationDone(target);104}105106pub fn history(ctx: Ctx) !void {107    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);108    defer ctx.gpa.free(gens);109    output.historyHeader(ctx.machine.name);110    for (gens) |g| output.historyRow(g);111}112113pub fn genCmd(ctx: Ctx) !void {114    if (std.mem.eql(u8, ctx.sub, "list")) return history(ctx);115    if (std.mem.eql(u8, ctx.sub, "current")) {116        const n = try exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine);117        output.genCurrent(n);118        return;119    }120    if (std.mem.eql(u8, ctx.sub, "delete")) {121        if (ctx.args.len == 0) return error.Internal;122        if (std.mem.eql(u8, ctx.args[0], "old")) {123            if (ctx.cfg.confirm and !output.confirm("")) return;124            _ = try exec.deleteOldGenerations(ctx.io, &ctx.machine, ctx.gpa);125        } else {126            const n = std.fmt.parseInt(u32, ctx.args[0], 10) catch return error.Internal;127            if (ctx.cfg.confirm and !output.confirm("")) return;128            _ = try exec.deleteGeneration(ctx.io, &ctx.machine, ctx.gpa, n);129        }130        output.done();131        return;132    }133    return error.Internal;134}135136pub fn clean(ctx: Ctx) !void {137    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);138    defer ctx.gpa.free(gens);139    output.cleanStart(ctx.machine.name, ctx.cfg.generations, @intCast(gens.len));140    if (ctx.cfg.confirm and !output.confirm("removing — cannot be undone")) return;141    const all = ctx.all or std.mem.eql(u8, ctx.sub, "all");142    _ = try exec.collectGarbage(ctx.io, &ctx.machine, ctx.gpa, all, ctx.cfg.generations);143    output.cleanDone("?");144}145146pub fn statusCmd(ctx: Ctx) !void {147    output.statusHeader();148    // --all renders every configured machine; remotes route through the same SSH149    // path as exec.capture. An unreachable machine is shown in red, not fatal.150    if (ctx.all and ctx.cfg.machines.len > 0) {151        for (ctx.cfg.machines) |m| statusRowFor(ctx, &m);152        return;153    }154    statusRowFor(ctx, &ctx.machine);155}156157fn statusRowFor(ctx: Ctx, machine: *const types.Machine) void {158    // A failure to read the current generation means we couldn't reach or query159    // the machine — mark it unreachable rather than reporting a bogus gen 0.160    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, machine) catch {161        output.statusRow(machine.name, 0, "", "", false);162        return;163    };164    const st = exec.systemStatus(ctx.gpa, ctx.io, machine);165    defer ctx.gpa.free(st.size);166    defer ctx.gpa.free(st.age);167    output.statusRow(machine.name, gen, st.age, st.size, true);168}169170pub fn diff(ctx: Ctx) !void {171    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);172    defer ctx.gpa.free(gens);173    var current: u32 = 0;174    var prev: u32 = 0;175    for (gens) |g| {176        if (g.current) current = g.number;177        if (!g.current and g.number > prev and g.number < current) prev = g.number;178    }179    if (ctx.args.len >= 2) {180        const a = std.fmt.parseInt(u32, ctx.args[0], 10) catch current - 1;181        const b = std.fmt.parseInt(u32, ctx.args[1], 10) catch current;182        output.diffHeader(a, b, ctx.machine.name);183        if (try exec.diffClosures(ctx.io, &ctx.machine, ctx.gpa, a, b) != 0) return error.ProcessFailed;184    } else {185        output.diffHeader(if (prev > 0) prev else current - 1, current, ctx.machine.name);186        if (try exec.diffClosures(ctx.io, &ctx.machine, ctx.gpa, if (prev > 0) prev else current - 1, current) != 0) return error.ProcessFailed;187    }188}189190pub fn mood(ctx: Ctx) !void {191    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine) catch 0;192    const st = exec.systemStatus(ctx.gpa, ctx.io, &ctx.machine);193    defer ctx.gpa.free(st.size);194    defer ctx.gpa.free(st.age);195    const state = exec.systemRunningState(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");196    defer ctx.gpa.free(state);197    output.mood(ctx.machine.name, state, gen, st.age, st.size);198}199200pub fn doctor(ctx: Ctx) !void {201    output.doctorHeader(ctx.machine.name);202    const checks = try exec.runDoctorChecks(ctx.gpa, ctx.io, &ctx.machine);203    defer ctx.gpa.free(checks);204    var warnings: u32 = 0;205    var failures: u32 = 0;206    for (checks) |ch| {207        output.doctorRow(ch);208        if (ch.status == .warn) warnings += 1;209        if (ch.status == .fail) failures += 1;210    }211    output.doctorSummary(warnings, failures);212}213214// nina update — kepr self-update for the nina binary itself.215pub fn update(ctx: Ctx) !void {216    output.updateSelfStart();217218    if (comptime std.mem.eql(u8, version.PLATFORM, "unsupported")) {219        output.updateSelfNoPlatform(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag));220        return error.Internal;221    }222223    const env_url = ctx.environ.get("NINA_UPDATE_SOURCE");224    const source_url = env_url orelse UPDATE_URL;225226    const body = exec.fetchUrl(ctx.gpa, ctx.io, source_url) catch |e| switch (e) {227        error.NetworkError, error.HttpError => {228            output.updateSelfNetworkError();229            return;230        },231        else => return e,232    };233    defer ctx.gpa.free(body);234235    const parsed = std.json.parseFromSlice(std.json.Value, ctx.gpa, body, .{}) catch {236        output.printWarning("could not parse release metadata");237        return;238    };239    defer parsed.deinit();240241    const root = parsed.value;242    if (root != .object) { output.printWarning("malformed release metadata"); return; }243244    const remote_ver = blk: {245        const v = root.object.get("version") orelse { output.printWarning("malformed release metadata"); return; };246        break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };247    };248249    if (std.mem.eql(u8, remote_ver, version.VERSION)) {250        output.updateSelfCurrent(version.VERSION);251        return;252    }253254    output.updateSelfAvailable(version.VERSION, remote_ver);255    if (ctx.check) return;256257    var exe_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;258    const install_path = exec.selfExePath(ctx.io, &exe_buf) catch {259        errors.error_info.setMessage("could not determine install path", .{});260        return error.Internal;261    };262263    // When running from the nix store, defer to nix profile upgrade so nix264    // manages the transition (the store is read-only; binary replacement fails).265    if (std.mem.startsWith(u8, install_path, "/nix/store/")) {266        output.updateSelfNixManaged(remote_ver);267        if (ctx.cfg.confirm and !output.confirm("")) return;268        const ec = exec.streamLocal(ctx.io, &.{ "nix", "profile", "upgrade", ".*nina.*" }) catch {269            output.updateSelfNixFailed();270            return;271        };272        if (ec != 0) output.updateSelfNixSystemManaged();273        return;274    }275276    const plat_info = blk: {277        const pv = root.object.get("platforms") orelse break :blk null;278        if (pv != .object) break :blk null;279        break :blk pv.object.get(version.PLATFORM);280    };281    if (plat_info == null) { output.updateSelfNoPlatform(version.PLATFORM); return; }282283    const dl_url = blk: {284        const v = plat_info.?.object.get("url") orelse { output.printWarning("malformed release metadata"); return; };285        break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };286    };287288    const raw_cksum = blk: {289        if (plat_info.?.object.get("blake3")) |v|290            break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };291        if (plat_info.?.object.get("checksum")) |v|292            break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };293        output.printWarning("missing checksum in release metadata");294        return;295    };296    const expected_hex = if (std.mem.startsWith(u8, raw_cksum, "blake3:")) raw_cksum["blake3:".len..] else raw_cksum;297298    if (ctx.cfg.confirm and !output.confirm("")) return;299300    output.updateSelfDownloading(remote_ver, version.PLATFORM);301    const tmp_path = exec.downloadToTemp(ctx.gpa, ctx.io, dl_url) catch {302        output.printWarning("download failed — check your connection and try again");303        return;304    };305    defer ctx.gpa.free(tmp_path);306307    output.updateSelfVerifying();308    exec.verifyAndInstall(ctx.gpa, ctx.io, tmp_path, expected_hex, install_path) catch |e| switch (e) {309        error.ChecksumMismatch => { output.updateSelfChecksumFailed(); return; },310        else => return e,311    };312313    output.updateSelfDone(version.VERSION, remote_ver);314}315316// nina goodbye — clean uninstall from nix profile or binary location.317pub fn goodbye(ctx: Ctx) !void {318    output.goodbyeStart();319320    var exe_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;321    const install_path = exec.selfExePath(ctx.io, &exe_buf) catch {322        errors.error_info.setMessage("could not determine install path", .{});323        return error.Internal;324    };325326    if (std.mem.startsWith(u8, install_path, "/nix/store/")) {327        output.goodbyeNix();328        if (ctx.cfg.confirm and !output.confirm("")) return;329        const ec = exec.streamLocal(ctx.io, &.{ "nix", "profile", "remove", ".*nina.*" }) catch {330            output.goodbyeFailed("nix profile remove failed");331            return;332        };333        if (ec != 0) {334            output.goodbyeNixSystemManaged();335            return;336        }337    } else {338        output.goodbyeFile(install_path);339        if (ctx.cfg.confirm and !output.confirm("")) return;340        std.Io.Dir.deleteFileAbsolute(ctx.io, install_path) catch |e| {341            errors.error_info.setMessage("could not remove {s}", .{install_path});342            return e;343        };344    }345346    // Offer to remove ~/nina.nix if it exists (generated by the installer).347    if (ctx.environ.get("HOME")) |home| {348        const nina_nix: ?[]u8 = std.fmt.allocPrint(ctx.gpa, "{s}/nina.nix", .{home}) catch null;349        if (nina_nix) |path| {350            defer ctx.gpa.free(path);351            const exists = blk: {352                const bytes = std.Io.Dir.readFileAlloc(.cwd(), ctx.io, path, ctx.gpa, .unlimited) catch break :blk false;353                ctx.gpa.free(bytes);354                break :blk true;355            };356            if (exists) {357                output.goodbyeNixFileFound(path);358                if (!ctx.cfg.confirm or output.confirm("")) {359                    std.Io.Dir.deleteFileAbsolute(ctx.io, path) catch {360                        output.printWarning("could not remove ~/nina.nix — remove it manually to clean up");361                    };362                }363            }364        }365    }366367    output.goodbyeDone();368}369370fn channelUpdateCmd(ctx: Ctx) !void {371    output.updateStart(ctx.machine.name);372    _ = try exec.channelUpdate(ctx.io, &ctx.machine, ctx.gpa);373    output.updateDone();374}375376pub fn upgrade(ctx: Ctx) !void {377    output.upgradeStart(ctx.machine.name);378    output.printSubstep("updating channels", .{});379    _ = exec.channelUpdate(ctx.io, &ctx.machine, ctx.gpa) catch {};380    output.printSubstep("rebuilding", .{});381    const start = std.Io.Clock.awake.now(ctx.io);382    const code = try exec.nixosRebuildSwitch(ctx.io, &ctx.machine, ctx.gpa);383    if (code != 0) {384        output.applyFailed("");385        return error.BuildFailed;386    }387    const elapsed_ns = @max(0, start.untilNow(ctx.io, .awake).toNanoseconds());388    const elapsed: u64 = @intCast(@divFloor(elapsed_ns, 1_000_000));389    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine) catch 0;390    output.applyDone(gen, elapsed, false);391}392393pub fn log(ctx: Ctx) !void {394    output.logHeader(ctx.machine.name);395    const content = exec.readNinaLog(ctx.gpa, ctx.io, ctx.environ, ctx.last) catch {396        // No log yet — that's fine397        return;398    };399    defer ctx.gpa.free(content);400    var lines = std.mem.splitScalar(u8, content, '\n');401    while (lines.next()) |line| {402        const t = std.mem.trim(u8, line, " \t\r");403        if (t.len == 0) continue;404        if (t[0] == '{') {405            renderJsonLogLine(ctx.gpa, t);406        } else {407            // legacy tab-separated format408            var parts = std.mem.tokenizeScalar(u8, t, '\t');409            const date = parts.next() orelse continue;410            const time = parts.next() orelse "";411            const action = parts.next() orelse "";412            const detail = parts.next() orelse "";413            output.logRow(date, time, action, detail);414        }415    }416}417418// The on-disk log is JSON Lines: {"ts","machine","command","outcome","gen_after",...}.419// Render ts -> date/time columns, command -> action, outcome(+gen) -> detail.420fn renderJsonLogLine(gpa: std.mem.Allocator, line: []const u8) void {421    const parsed = std.json.parseFromSlice(std.json.Value, gpa, line, .{}) catch {422        output.logRow(line, "", "", ""); // unparseable — show raw rather than drop423        return;424    };425    defer parsed.deinit();426    if (parsed.value != .object) return;427    const o = parsed.value.object;428429    const ts = jsonStr(o, "ts");430    const date = if (ts.len >= 10) ts[0..10] else ts;431    const time = if (ts.len >= 16) ts[11..16] else "";432    const action = jsonStr(o, "command");433    const outcome = jsonStr(o, "outcome");434435    var buf: [160]u8 = undefined;436    var detail: []const u8 = outcome;437    if (o.get("gen_after")) |g| {438        if (g == .integer) {439            detail = std.fmt.bufPrint(&buf, "{s}   -> gen {d}", .{ outcome, g.integer }) catch outcome;440        }441    }442    output.logRow(date, time, action, detail);443}444445fn jsonStr(o: std.json.ObjectMap, key: []const u8) []const u8 {446    if (o.get(key)) |v| {447        if (v == .string) return v.string;448    }449    return "";450}451452pub fn edit(ctx: Ctx) !void {453    const hardware = ctx.args.len > 0 and std.mem.eql(u8, ctx.args[0], "hardware");454    const filename = if (hardware) "hardware-configuration.nix" else "configuration.nix";455    const path = try std.fmt.allocPrint(ctx.gpa, "{s}/{s}", .{ ctx.machine.config_path, filename });456    defer ctx.gpa.free(path);457    output.editOpen(path, ctx.cfg.editor);458    _ = try exec.openEditor(ctx.io, ctx.gpa, ctx.cfg.editor, path, null);459}460461pub fn fmt(ctx: Ctx) !void {462    const path = try std.fmt.allocPrint(ctx.gpa, "{s}/configuration.nix", .{ctx.machine.config_path});463    defer ctx.gpa.free(path);464    if (ctx.check) {465        const code = try exec.nixpkgsFmt(ctx.io, &ctx.machine, ctx.gpa, path, true);466        if (code != 0) output.fmtNeedsFormat() else output.fmtDone();467        return;468    }469    output.fmtStart(path);470    _ = try exec.nixpkgsFmt(ctx.io, &ctx.machine, ctx.gpa, path, false);471    output.fmtDone();472}473474pub fn repl(ctx: Ctx) !void {475    output.replStart();476    _ = try exec.nixRepl(ctx.io, &ctx.machine, ctx.gpa);477}478479pub fn develop(ctx: Ctx) !void {480    if (std.mem.eql(u8, ctx.sub, "show")) {481        _ = try exec.nixBuild(ctx.io, &ctx.machine, ctx.gpa, null);482        return;483    }484    const run_cmd: ?[]const u8 = if (ctx.args.len > 0 and std.mem.eql(u8, ctx.sub, "run"))485        ctx.args[0]486    else487        null;488    output.developStart(&.{});489    _ = try exec.nixDevelop(ctx.io, &ctx.machine, ctx.gpa, run_cmd);490    output.developDone();491}492493pub fn info(ctx: Ctx) !void {494    output.infoHeader(ctx.machine.name);495    const nixos_ver = exec.systemInfo(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");496    defer ctx.gpa.free(nixos_ver);497    output.infoRow("nixos", std.mem.trim(u8, nixos_ver, " \t\n\r"));498499    const kernel = exec.kernelInfo(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");500    defer ctx.gpa.free(kernel);501    output.infoRow("kernel", std.mem.trim(u8, kernel, " \t\n\r"));502503    const uptime = exec.uptimeInfo(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");504    defer ctx.gpa.free(uptime);505    output.infoRow("uptime", std.mem.trim(u8, uptime, " \t\n\r"));506}507508pub fn boot(ctx: Ctx) !void {509    output.bootHeader(ctx.machine.name);510    const entries = exec.bootEntries(ctx.gpa, ctx.io, &ctx.machine) catch &.{};511    defer ctx.gpa.free(entries);512    for (entries, 0..) |entry, i| output.bootEntry(entry, i);513}514515pub fn searchCmd(ctx: Ctx) !void {516    const query = if (ctx.args.len > 0) ctx.args[0] else "";517    api.ensureRegistryPinned(ctx.gpa, ctx.io, ctx.environ);518    const result = try search_tui.run(ctx.gpa, ctx.io, query, .packages);519    if (result) |r| {520        installFromResult(ctx, r) catch |e| return e;521    }522}523524pub fn optionCmd(ctx: Ctx) !void {525    const query = if (ctx.args.len > 0) ctx.args[0] else "";526    api.ensureRegistryPinned(ctx.gpa, ctx.io, ctx.environ);527    _ = try search_tui.run(ctx.gpa, ctx.io, query, .options);528}529530pub fn install(ctx: Ctx) !void {531    const pkg_name = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;532533    api.ensureRegistryPinned(ctx.gpa, ctx.io, ctx.environ);534535    // Search for the package to get the attr536    const pkgs = api.searchPackages(ctx.gpa, ctx.io, pkg_name) catch {537        errors.error_info.setMessage("'{s}' not found in nixpkgs", .{pkg_name});538        errors.error_info.setSuggestion("nina search {s}", .{pkg_name});539        return error.PackageNotFound;540    };541    defer ctx.gpa.free(pkgs);542543    if (pkgs.len == 0) {544        errors.error_info.setMessage("'{s}' not found in nixpkgs", .{pkg_name});545        errors.error_info.setSuggestion("nina search {s}", .{pkg_name});546        return error.PackageNotFound;547    }548549    // Prefer an exact name match over the first search result — `install ripgrep`550    // must pick "ripgrep", not the alphabetically-first match like "repgrep".551    var found = pkgs[0];552    for (pkgs) |p| {553        if (std.mem.eql(u8, p.pname, pkg_name)) {554            found = p;555            break;556        }557    }558    output.installSelectPrompt(found.pname, found.version);559560    var choice_buf: [8]u8 = undefined;561    var choice_len: usize = 0;562    while (choice_len < choice_buf.len) {563        const n = std.posix.read(std.posix.STDIN_FILENO, choice_buf[choice_len .. choice_len + 1]) catch break;564        if (n == 0) break;565        if (choice_buf[choice_len] == '\n') break;566        choice_len += 1;567    }568    const choice = std.mem.trim(u8, choice_buf[0..choice_len], " \t");569570    if (choice.len == 0 or choice[0] == 'i' or choice[0] == 'I') {571        try profileInstallPkg(ctx, found);572    } else if (choice[0] == 's' or choice[0] == 'S') {573        try systemInstallPkg(ctx, found);574    } else if (choice[0] == 't' or choice[0] == 'T') {575        try tryPkg(ctx, found);576    }577}578579fn installFromResult(ctx: Ctx, result: types.SearchResult) !void {580    switch (result.action) {581        .profile => try profileInstallPkg(ctx, result.package),582        .system => try systemInstallPkg(ctx, result.package),583    }584}585586fn profileInstallPkg(ctx: Ctx, npkg: types.NixPackage) !void {587    output.installStart(npkg.pname, .profile);588    output.printSubstep("nix profile install nixpkgs#{s}", .{npkg.attr});589    output.printTeach(try std.fmt.allocPrint(ctx.gpa, "nix profile install nixpkgs#{s}", .{npkg.attr}));590    const code = try exec.profileInstall(ctx.io, &ctx.machine, ctx.gpa, npkg.attr);591    if (code != 0) return error.ProcessFailed;592    output.installDone(npkg.pname);593}594595fn systemInstallPkg(ctx: Ctx, npkg: types.NixPackage) !void {596    const config_path = try std.fmt.allocPrint(ctx.gpa, "{s}/configuration.nix", .{ctx.machine.config_path});597    defer ctx.gpa.free(config_path);598    const line = api.findSystemPackagesLine(ctx.io, config_path) orelse 1;599    output.installEditorPrompt(npkg.pname, config_path, line, ctx.cfg.editor);600    _ = try exec.openEditor(ctx.io, ctx.gpa, ctx.cfg.editor, config_path, line);601    if (ctx.cfg.confirm) {602        if (output.confirm("apply now? ")) {603            try apply(ctx);604        }605    }606}607608fn tryPkg(ctx: Ctx, npkg: types.NixPackage) !void {609    output.tryStart(npkg.pname);610    output.printSubstep("nix shell nixpkgs#{s}", .{npkg.attr});611    _ = try exec.nixShell(ctx.io, &ctx.machine, ctx.gpa, npkg.attr);612    output.tryDone();613}614615pub fn remove(ctx: Ctx) !void {616    const pkg_name = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;617    output.removeStart(pkg_name);618    const code = try exec.profileRemove(ctx.io, &ctx.machine, ctx.gpa, pkg_name);619    if (code != 0) return error.ProcessFailed;620    output.done();621}622623pub fn list(ctx: Ctx) !void {624    output.listHeader();625    const profile_out = exec.profileList(ctx.gpa, ctx.io, &ctx.machine) catch null;626    if (profile_out) |out| {627        defer ctx.gpa.free(out);628        output.listSection("profile");629        var lines = std.mem.splitScalar(u8, out, '\n');630        while (lines.next()) |line| {631            const t = std.mem.trim(u8, line, " \t\r");632            if (t.len == 0) continue;633            output.listPkg(t, "");634        }635    }636}637638pub fn tryCmd(ctx: Ctx) !void {639    const pkg_name_arg = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;640    const shell_pkg = types.NixPackage{ .attr = pkg_name_arg, .pname = pkg_name_arg, .version = "", .description = "" };641    try tryPkg(ctx, shell_pkg);642}643644// --- Service commands ---645646pub fn service(ctx: Ctx) !void {647    if (std.mem.eql(u8, ctx.sub, "list") or ctx.sub.len == 0) {648        output.serviceListHeader(ctx.machine.name);649        const svcs = exec.serviceList(ctx.gpa, ctx.io, &ctx.machine) catch &.{};650        defer ctx.gpa.free(svcs);651        for (svcs) |s| output.serviceRow(s);652        return;653    }654    if (std.mem.eql(u8, ctx.sub, "logs")) {655        const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;656        const follow = ctx.args.len > 1 and std.mem.eql(u8, ctx.args[1], "-f");657        output.serviceLogsHeader(name, ctx.last, ctx.machine.name);658        _ = try exec.serviceLogs(ctx.io, &ctx.machine, ctx.gpa, name, ctx.last, follow);659        return;660    }661    if (std.mem.eql(u8, ctx.sub, "status")) {662        const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;663        output.serviceVerb("status", name, ctx.machine.name);664        _ = try exec.serviceCtl(ctx.io, &ctx.machine, ctx.gpa, "status", name);665        return;666    }667    // start/stop/restart/enable/disable668    const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;669    if (ctx.cfg.confirm and (std.mem.eql(u8, ctx.sub, "stop") or std.mem.eql(u8, ctx.sub, "disable"))) {670        if (!output.confirm("")) return;671    }672    output.serviceVerb(ctx.sub, name, ctx.machine.name);673    _ = try exec.serviceCtl(ctx.io, &ctx.machine, ctx.gpa, ctx.sub, name);674    output.serviceDone();675}676677// --- Flake commands ---678679pub fn flake(ctx: Ctx) !void {680    if (std.mem.eql(u8, ctx.sub, "update")) {681        output.flakeUpdateStart();682        const input: ?[]const u8 = if (ctx.args.len > 0) ctx.args[0] else null;683        _ = try exec.flakeUpdate(ctx.io, &ctx.machine, ctx.gpa, input);684        output.flakeLockWritten();685        return;686    }687    if (std.mem.eql(u8, ctx.sub, "check")) {688        output.flakeCheckStart();689        const code = try exec.flakeCheck(ctx.io, &ctx.machine, ctx.gpa);690        if (code == 0) output.flakeValid() else output.checkFailed("");691        return;692    }693    if (std.mem.eql(u8, ctx.sub, "show")) {694        const json_out = exec.flakeShow(ctx.gpa, ctx.io, &ctx.machine) catch null;695        if (json_out) |j| {696            defer ctx.gpa.free(j);697            output.flakeShowHeader("flake outputs");698            // Parse and display the flake outputs699            const parsed = std.json.parseFromSlice(std.json.Value, ctx.gpa, j, .{}) catch return;700            defer parsed.deinit();701            if (parsed.value == .object) {702                var it = parsed.value.object.iterator();703                while (it.next()) |entry| {704                    if (entry.value_ptr.* == .object) {705                        var inner = entry.value_ptr.object.iterator();706                        while (inner.next()) |e2| {707                            const desc = if (e2.value_ptr.* == .object)708                                if (e2.value_ptr.object.get("description")) |d|709                                    if (d == .string) d.string else ""710                                else ""711                            else "";712                            output.flakeShowEntry(entry.key_ptr.*, e2.key_ptr.*, desc);713                        }714                    }715                }716            }717        }718        return;719    }720    if (std.mem.eql(u8, ctx.sub, "init")) {721        _ = try exec.flakeInit(ctx.io, &ctx.machine, ctx.gpa);722        return;723    }724    if (std.mem.eql(u8, ctx.sub, "lock")) {725        _ = try exec.flakeLock(ctx.io, &ctx.machine, ctx.gpa);726        return;727    }728    if (std.mem.eql(u8, ctx.sub, "clone")) {729        const url = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;730        _ = try exec.flakeClone(ctx.io, &ctx.machine, ctx.gpa, url);731        return;732    }733}734735// --- Store commands ---736737pub fn store(ctx: Ctx) !void {738    if (std.mem.eql(u8, ctx.sub, "gc")) {739        _ = try exec.storeGc(ctx.io, &ctx.machine, ctx.gpa);740        return;741    }742    if (std.mem.eql(u8, ctx.sub, "verify")) {743        output.storeVerifyStart();744        _ = try exec.storeVerify(ctx.io, &ctx.machine, ctx.gpa);745        return;746    }747    if (std.mem.eql(u8, ctx.sub, "repair")) {748        if (ctx.cfg.confirm and !output.confirm("")) return;749        output.storeRepairStart();750        _ = try exec.storeRepair(ctx.io, &ctx.machine, ctx.gpa);751        return;752    }753    if (std.mem.eql(u8, ctx.sub, "info")) {754        const si = try exec.storeInfo(ctx.gpa, ctx.io, &ctx.machine);755        output.storeInfo(si);756        return;757    }758    if (std.mem.eql(u8, ctx.sub, "path")) {759        const attr = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;760        const path = try exec.storePath(ctx.gpa, ctx.io, &ctx.machine, attr);761        defer ctx.gpa.free(path);762        output.storePath(path);763        return;764    }765    // default: info766    const si = try exec.storeInfo(ctx.gpa, ctx.io, &ctx.machine);767    output.storeInfo(si);768}769770// --- Channel commands ---771772pub fn channel(ctx: Ctx) !void {773    if (std.mem.eql(u8, ctx.sub, "list") or ctx.sub.len == 0) {774        output.channelListHeader(ctx.machine.name);775        const chans = try exec.channelList(ctx.gpa, ctx.io, &ctx.machine);776        defer ctx.gpa.free(chans);777        for (chans) |ch| output.channelRow(ch);778        return;779    }780    if (std.mem.eql(u8, ctx.sub, "add")) {781        const url = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;782        const name = if (ctx.args.len > 1) ctx.args[1] else "";783        _ = try exec.channelAdd(ctx.io, &ctx.machine, ctx.gpa, url, name);784        output.channelDone();785        return;786    }787    if (std.mem.eql(u8, ctx.sub, "remove")) {788        const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;789        _ = try exec.channelRemove(ctx.io, &ctx.machine, ctx.gpa, name);790        output.channelDone();791        return;792    }793    if (std.mem.eql(u8, ctx.sub, "update")) {794        return channelUpdateCmd(ctx);795    }796}797798// --- Profile commands ---799800pub fn profile(ctx: Ctx) !void {801    if (std.mem.eql(u8, ctx.sub, "list") or ctx.sub.len == 0) {802        output.profileListHeader();803        const out = exec.profileList(ctx.gpa, ctx.io, &ctx.machine) catch null;804        if (out) |o| {805            defer ctx.gpa.free(o);806            var lines = std.mem.splitScalar(u8, o, '\n');807            while (lines.next()) |line| {808                const t = std.mem.trim(u8, line, " \t\r");809                if (t.len == 0) continue;810                output.profileRow(t, "", "");811            }812        }813        return;814    }815    if (std.mem.eql(u8, ctx.sub, "install")) {816        const attr = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;817        _ = try exec.profileInstall(ctx.io, &ctx.machine, ctx.gpa, attr);818        output.done();819        return;820    }821    if (std.mem.eql(u8, ctx.sub, "remove")) {822        const attr = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;823        _ = try exec.profileRemove(ctx.io, &ctx.machine, ctx.gpa, attr);824        output.done();825        return;826    }827    if (std.mem.eql(u8, ctx.sub, "upgrade")) {828        _ = try exec.profileUpgrade(ctx.io, &ctx.machine, ctx.gpa);829        output.done();830        return;831    }832}833834// --- Pkg commands ---835836pub fn pkg(ctx: Ctx) !void {837    const name = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;838    if (std.mem.eql(u8, ctx.sub, "why")) {839        _ = try exec.pkgWhy(ctx.io, &ctx.machine, ctx.gpa, name);840        return;841    }842    if (std.mem.eql(u8, ctx.sub, "deps")) {843        _ = try exec.pkgDeps(ctx.io, &ctx.machine, ctx.gpa, name);844        return;845    }846    if (std.mem.eql(u8, ctx.sub, "size")) {847        const s = exec.pkgSize(ctx.gpa, ctx.io, &ctx.machine, name) catch null;848        if (s) |sz| {849            defer ctx.gpa.free(sz);850            output.pkgResult("size", sz);851        }852        return;853    }854    if (std.mem.eql(u8, ctx.sub, "path")) {855        const p = try exec.storePath(ctx.gpa, ctx.io, &ctx.machine, name);856        defer ctx.gpa.free(p);857        output.pkgResult("path", p);858        return;859    }860    if (std.mem.eql(u8, ctx.sub, "closure")) {861        _ = try exec.pkgClosure(ctx.io, &ctx.machine, ctx.gpa, name);862        return;863    }864}865866// --- Pin / Unpin ---867868pub fn pin(ctx: Ctx) !void {869    const input = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;870    const commit = if (ctx.args.len > 1) ctx.args[1] else return error.Internal;871    exec.editFlakeLock(ctx.gpa, input, commit) catch return error.ProcessFailed;872    output.pinDone(input, commit);873}874875pub fn unpin(ctx: Ctx) !void {876    const input = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;877    _ = try exec.flakeUpdate(ctx.io, &ctx.machine, ctx.gpa, input);878    output.unpinDone(input);879}880881// --- Hash / Fetch ---882883pub fn hash(ctx: Ctx) !void {884    const path = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;885    const h = try exec.nixHash(ctx.gpa, ctx.io, &ctx.machine, path);886    defer ctx.gpa.free(h);887    output.hashResult(std.mem.trim(u8, h, " \t\n\r"));888}889890pub fn fetch(ctx: Ctx) !void {891    const url = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;892    const h = try exec.nixFetch(ctx.gpa, ctx.io, &ctx.machine, url);893    defer ctx.gpa.free(h);894    output.fetchResult(std.mem.trim(u8, h, " \t\n\r"), url);895}896897// --- Build / Run ---898899pub fn build(ctx: Ctx) !void {900    const attr: ?[]const u8 = if (ctx.args.len > 0) ctx.args[0] else null;901    output.buildStart(attr orelse "default");902    const code = try exec.nixBuild(ctx.io, &ctx.machine, ctx.gpa, attr);903    if (code != 0) return error.BuildFailed;904    output.buildDone();905}906907pub fn runCmd(ctx: Ctx) !void {908    const pkg_arg = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;909    output.runStart(pkg_arg);910    _ = try exec.nixRun(ctx.io, &ctx.machine, ctx.gpa, pkg_arg);911}912913// --- Hello ---914915pub fn hello(ctx: Ctx) !void {916    output.hello(ctx.cfg.machines);917}918919pub fn setup(gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map) !void {920    output.setupCheck();921    const status = exec.checkNixFeatures(gpa, io, environ);922    output.setupFeatures(status.nix_command, status.flakes);923924    if (status.allEnabled()) {925        output.setupAllSet();926        api.ensureRegistryPinned(gpa, io, environ);927        return;928    }929930    output.setupEnabling();931    const path = try exec.enableNixFeatures(gpa, io, environ);932    defer gpa.free(path);933934    output.setupDone(path, exec.isNixOS(io));935    api.ensureRegistryPinned(gpa, io, environ);936}937938// --- Log helper ---939940fn logAction(ctx: Ctx, action: []const u8, gen: u32) void {941    var ts_buf: [32]u8 = undefined;942    const ts = nowIso(ctx.io, &ts_buf);943    var buf: [256]u8 = undefined;944    const entry = std.fmt.bufPrint(&buf, "{{\"ts\":\"{s}\",\"machine\":\"{s}\",\"command\":\"{s}\",\"outcome\":\"success\",\"gen_before\":null,\"gen_after\":{d},\"duration_ms\":0}}\n", .{945        ts, ctx.machine.name, action, gen,946    }) catch return;947    exec.appendNinaLog(ctx.io, ctx.environ, entry);948}949950// Wall-clock UTC as ISO-8601. Uses Io.Clock.real (the settable Unix-epoch clock);951// Io.Clock.awake is monotonic and unsuitable for timestamps. Falls back to epoch 0.952fn nowIso(io: std.Io, buf: []u8) []const u8 {953    const fallback = "1970-01-01T00:00:00Z";954    const ns = std.Io.Clock.real.now(io).toNanoseconds();955    if (ns <= 0) return fallback;956    const secs: u64 = @intCast(@divFloor(ns, 1_000_000_000));957    const es = std.time.epoch.EpochSeconds{ .secs = secs };958    const ed = es.getEpochDay();959    const yd = ed.calculateYearDay();960    const md = yd.calculateMonthDay();961    const ds = es.getDaySeconds();962    return std.fmt.bufPrint(buf, "{d:0>4}-{d:0>2}-{d:0>2}T{d:0>2}:{d:0>2}:{d:0>2}Z", .{963        yd.year, md.month.numeric(), @as(u32, md.day_index) + 1,964        ds.getHoursIntoDay(), ds.getMinutesIntoHour(), ds.getSecondsIntoMinute(),965    }) catch return fallback;966}