commands.zig

43.0 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; };8384    // Newest generation older than current. If there is none, we're already at the85    // oldest — stop cleanly instead of letting rollback fail with 'build failed'.86    var prev: u32 = 0;87    var has_prev = false;88    for (gens) |g| {89        if (g.number < current and (!has_prev or g.number > prev)) {90            prev = g.number;91            has_prev = true;92        }93    }94    if (!has_prev) {95        output.alreadyOldest();96        return;97    }9899    output.backStart(ctx.machine.name, current, prev);100    if (ctx.cfg.confirm and !output.confirm("")) return;101102    const code = try exec.rollback(ctx.io, &ctx.machine, ctx.gpa);103    if (code != 0) return error.BuildFailed;104    output.backDone(prev);105}106107pub fn go(ctx: Ctx) !void {108    if (ctx.args.len == 0) return error.Internal;109    const target = std.fmt.parseInt(u32, ctx.args[0], 10) catch return error.Internal;110    const current = try exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine);111112    output.goStart(ctx.machine.name, current, target);113    if (ctx.cfg.confirm and !output.confirm("")) return;114115    const code = try exec.switchGeneration(ctx.io, &ctx.machine, ctx.gpa, target);116    if (code != 0) return error.ProcessFailed;117    output.generationDone(target);118}119120pub fn history(ctx: Ctx) !void {121    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);122    defer ctx.gpa.free(gens);123    output.historyHeader(ctx.machine.name);124    for (gens) |g| output.historyRow(g);125}126127pub fn genCmd(ctx: Ctx) !void {128    if (std.mem.eql(u8, ctx.sub, "list")) return history(ctx);129    if (std.mem.eql(u8, ctx.sub, "current")) {130        const n = try exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine);131        output.genCurrent(n);132        return;133    }134    if (std.mem.eql(u8, ctx.sub, "delete")) {135        if (ctx.args.len == 0) return error.Internal;136        if (std.mem.eql(u8, ctx.args[0], "old")) {137            if (ctx.cfg.confirm and !output.confirm("")) return;138            _ = try exec.deleteOldGenerations(ctx.io, &ctx.machine, ctx.gpa);139        } else {140            const n = std.fmt.parseInt(u32, ctx.args[0], 10) catch return error.Internal;141            if (ctx.cfg.confirm and !output.confirm("")) return;142            _ = try exec.deleteGeneration(ctx.io, &ctx.machine, ctx.gpa, n);143        }144        output.done();145        return;146    }147    return error.Internal;148}149150pub fn clean(ctx: Ctx) !void {151    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);152    defer ctx.gpa.free(gens);153    const all = ctx.all or std.mem.eql(u8, ctx.sub, "all");154    const total: u32 = @intCast(gens.len);155    // `clean all` keeps only the current generation; otherwise keep the configured156    // count. With nothing older than the keep limit, there's nothing to collect.157    const keep: u32 = if (all) 1 else ctx.cfg.generations;158    if (total <= keep) {159        output.cleanNothing(ctx.machine.name, total);160        return;161    }162    output.cleanStart(ctx.machine.name, keep, total);163    if (ctx.cfg.confirm and !output.confirm("removing — cannot be undone")) return;164    _ = try exec.collectGarbage(ctx.io, &ctx.machine, ctx.gpa, all, ctx.cfg.generations);165    output.cleanDone("?");166}167168pub fn statusCmd(ctx: Ctx) !void {169    output.statusHeader();170    // --all renders every configured machine; remotes route through the same SSH171    // path as exec.capture. An unreachable machine is shown in red, not fatal.172    if (ctx.all and ctx.cfg.machines.len > 0) {173        for (ctx.cfg.machines) |m| statusRowFor(ctx, &m);174        return;175    }176    statusRowFor(ctx, &ctx.machine);177}178179fn statusRowFor(ctx: Ctx, machine: *const types.Machine) void {180    // A failure to read the current generation means we couldn't reach or query181    // the machine — mark it unreachable rather than reporting a bogus gen 0.182    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, machine) catch {183        output.statusRow(machine.name, 0, "", "", false);184        return;185    };186    const st = exec.systemStatus(ctx.gpa, ctx.io, machine);187    defer ctx.gpa.free(st.size);188    defer ctx.gpa.free(st.age);189    output.statusRow(machine.name, gen, st.age, st.size, true);190}191192pub fn diff(ctx: Ctx) !void {193    const gens = try exec.getGenerations(ctx.gpa, ctx.io, &ctx.machine);194    defer ctx.gpa.free(gens);195    var current: u32 = 0;196    var prev: u32 = 0;197    for (gens) |g| {198        if (g.current) current = g.number;199        if (!g.current and g.number > prev and g.number < current) prev = g.number;200    }201    if (ctx.args.len >= 2) {202        const a = std.fmt.parseInt(u32, ctx.args[0], 10) catch current - 1;203        const b = std.fmt.parseInt(u32, ctx.args[1], 10) catch current;204        output.diffHeader(a, b, ctx.machine.name);205        if (try exec.diffClosures(ctx.io, &ctx.machine, ctx.gpa, a, b) != 0) return error.ProcessFailed;206    } else {207        output.diffHeader(if (prev > 0) prev else current - 1, current, ctx.machine.name);208        if (try exec.diffClosures(ctx.io, &ctx.machine, ctx.gpa, if (prev > 0) prev else current - 1, current) != 0) return error.ProcessFailed;209    }210}211212pub fn mood(ctx: Ctx) !void {213    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine) catch 0;214    const st = exec.systemStatus(ctx.gpa, ctx.io, &ctx.machine);215    defer ctx.gpa.free(st.size);216    defer ctx.gpa.free(st.age);217    const state = exec.systemRunningState(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");218    defer ctx.gpa.free(state);219    output.mood(ctx.machine.name, state, gen, st.age, st.size);220}221222pub fn doctor(ctx: Ctx) !void {223    output.doctorHeader(ctx.machine.name);224    const checks = try exec.runDoctorChecks(ctx.gpa, ctx.io, &ctx.machine);225    defer ctx.gpa.free(checks);226    var warnings: u32 = 0;227    var failures: u32 = 0;228    for (checks) |ch| {229        output.doctorRow(ch);230        if (ch.status == .warn) warnings += 1;231        if (ch.status == .fail) failures += 1;232    }233    output.doctorSummary(warnings, failures);234}235236// nina update — kepr self-update for the nina binary itself.237pub fn update(ctx: Ctx) !void {238    output.updateSelfStart();239240    if (comptime std.mem.eql(u8, version.PLATFORM, "unsupported")) {241        output.updateSelfNoPlatform(@tagName(builtin.cpu.arch) ++ "-" ++ @tagName(builtin.os.tag));242        return error.Internal;243    }244245    const env_url = ctx.environ.get("NINA_UPDATE_SOURCE");246    const source_url = env_url orelse UPDATE_URL;247248    const body = exec.fetchUrl(ctx.gpa, ctx.io, source_url) catch |e| switch (e) {249        error.NetworkError, error.HttpError => {250            output.updateSelfNetworkError();251            return;252        },253        else => return e,254    };255    defer ctx.gpa.free(body);256257    const parsed = std.json.parseFromSlice(std.json.Value, ctx.gpa, body, .{}) catch {258        output.printWarning("could not parse release metadata");259        return;260    };261    defer parsed.deinit();262263    const root = parsed.value;264    if (root != .object) { output.printWarning("malformed release metadata"); return; }265266    const remote_ver = blk: {267        const v = root.object.get("version") orelse { output.printWarning("malformed release metadata"); return; };268        break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };269    };270271    if (std.mem.eql(u8, remote_ver, version.VERSION)) {272        output.updateSelfCurrent(version.VERSION);273        return;274    }275276    output.updateSelfAvailable(version.VERSION, remote_ver);277    if (ctx.check) return;278279    var exe_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;280    const install_path = exec.selfExePath(ctx.io, &exe_buf) catch {281        errors.error_info.setMessage("could not determine install path", .{});282        return error.Internal;283    };284285    // When running from the nix store, defer to nix profile upgrade so nix286    // manages the transition (the store is read-only; binary replacement fails).287    if (std.mem.startsWith(u8, install_path, "/nix/store/")) {288        output.updateSelfNixManaged(remote_ver);289        if (ctx.cfg.confirm and !output.confirm("")) return;290        const ec = exec.streamLocal(ctx.io, &.{ "nix", "profile", "upgrade", ".*nina.*" }) catch {291            output.updateSelfNixFailed();292            return;293        };294        if (ec != 0) output.updateSelfNixSystemManaged();295        return;296    }297298    const plat_info = blk: {299        const pv = root.object.get("platforms") orelse break :blk null;300        if (pv != .object) break :blk null;301        break :blk pv.object.get(version.PLATFORM);302    };303    if (plat_info == null) { output.updateSelfNoPlatform(version.PLATFORM); return; }304305    const dl_url = blk: {306        const v = plat_info.?.object.get("url") orelse { output.printWarning("malformed release metadata"); return; };307        break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };308    };309310    const raw_cksum = blk: {311        if (plat_info.?.object.get("blake3")) |v|312            break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };313        if (plat_info.?.object.get("checksum")) |v|314            break :blk switch (v) { .string => |s| s, else => { output.printWarning("malformed release metadata"); return; } };315        output.printWarning("missing checksum in release metadata");316        return;317    };318    const expected_hex = if (std.mem.startsWith(u8, raw_cksum, "blake3:")) raw_cksum["blake3:".len..] else raw_cksum;319320    if (ctx.cfg.confirm and !output.confirm("")) return;321322    output.updateSelfDownloading(remote_ver, version.PLATFORM);323    const tmp_path = exec.downloadToTemp(ctx.gpa, ctx.io, dl_url) catch {324        output.printWarning("download failed — check your connection and try again");325        return;326    };327    defer ctx.gpa.free(tmp_path);328329    output.updateSelfVerifying();330    exec.verifyAndInstall(ctx.gpa, ctx.io, tmp_path, expected_hex, install_path) catch |e| switch (e) {331        error.ChecksumMismatch => { output.updateSelfChecksumFailed(); return; },332        else => return e,333    };334335    output.updateSelfDone(version.VERSION, remote_ver);336}337338// nina goodbye — clean uninstall from nix profile or binary location.339pub fn goodbye(ctx: Ctx) !void {340    output.goodbyeStart();341342    var exe_buf: [std.Io.Dir.max_path_bytes]u8 = undefined;343    const install_path = exec.selfExePath(ctx.io, &exe_buf) catch {344        errors.error_info.setMessage("could not determine install path", .{});345        return error.Internal;346    };347348    if (std.mem.startsWith(u8, install_path, "/nix/store/")) {349        output.goodbyeNix();350        if (ctx.cfg.confirm and !output.confirm("")) return;351        const ec = exec.streamLocal(ctx.io, &.{ "nix", "profile", "remove", ".*nina.*" }) catch {352            output.goodbyeFailed("nix profile remove failed");353            return;354        };355        if (ec != 0) {356            output.goodbyeNixSystemManaged();357            return;358        }359    } else {360        output.goodbyeFile(install_path);361        if (ctx.cfg.confirm and !output.confirm("")) return;362        std.Io.Dir.deleteFileAbsolute(ctx.io, install_path) catch |e| {363            errors.error_info.setMessage("could not remove {s}", .{install_path});364            return e;365        };366    }367368    // Offer to remove ~/nina.nix if it exists (generated by the installer).369    if (ctx.environ.get("HOME")) |home| {370        const nina_nix: ?[]u8 = std.fmt.allocPrint(ctx.gpa, "{s}/nina.nix", .{home}) catch null;371        if (nina_nix) |path| {372            defer ctx.gpa.free(path);373            const exists = blk: {374                const bytes = std.Io.Dir.readFileAlloc(.cwd(), ctx.io, path, ctx.gpa, .unlimited) catch break :blk false;375                ctx.gpa.free(bytes);376                break :blk true;377            };378            if (exists) {379                output.goodbyeNixFileFound(path);380                if (!ctx.cfg.confirm or output.confirm("")) {381                    std.Io.Dir.deleteFileAbsolute(ctx.io, path) catch {382                        output.printWarning("could not remove ~/nina.nix — remove it manually to clean up");383                    };384                }385            }386        }387    }388389    output.goodbyeDone();390}391392fn channelUpdateCmd(ctx: Ctx) !void {393    output.updateStart(ctx.machine.name);394    _ = try exec.channelUpdate(ctx.io, &ctx.machine, ctx.gpa);395    output.updateDone();396}397398pub fn upgrade(ctx: Ctx) !void {399    output.upgradeStart(ctx.machine.name);400    output.printSubstep("updating channels", .{});401    _ = exec.channelUpdate(ctx.io, &ctx.machine, ctx.gpa) catch {};402    output.printSubstep("rebuilding", .{});403    const start = std.Io.Clock.awake.now(ctx.io);404    const code = try exec.nixosRebuildSwitch(ctx.io, &ctx.machine, ctx.gpa);405    if (code != 0) {406        output.applyFailed("");407        return error.BuildFailed;408    }409    const elapsed_ns = @max(0, start.untilNow(ctx.io, .awake).toNanoseconds());410    const elapsed: u64 = @intCast(@divFloor(elapsed_ns, 1_000_000));411    const gen = exec.getCurrentGeneration(ctx.gpa, ctx.io, &ctx.machine) catch 0;412    output.applyDone(gen, elapsed, false);413}414415pub fn log(ctx: Ctx) !void {416    output.logHeader(ctx.machine.name);417    const content = exec.readNinaLog(ctx.gpa, ctx.io, ctx.environ, ctx.last) catch {418        // No log yet — that's fine419        return;420    };421    defer ctx.gpa.free(content);422    var lines = std.mem.splitScalar(u8, content, '\n');423    while (lines.next()) |line| {424        const t = std.mem.trim(u8, line, " \t\r");425        if (t.len == 0) continue;426        if (t[0] == '{') {427            renderJsonLogLine(ctx.gpa, t);428        } else {429            // legacy tab-separated format430            var parts = std.mem.tokenizeScalar(u8, t, '\t');431            const date = parts.next() orelse continue;432            const time = parts.next() orelse "";433            const action = parts.next() orelse "";434            const detail = parts.next() orelse "";435            output.logRow(date, time, action, detail);436        }437    }438}439440// The on-disk log is JSON Lines: {"ts","machine","command","outcome","gen_after",...}.441// Render ts -> date/time columns, command -> action, outcome(+gen) -> detail.442fn renderJsonLogLine(gpa: std.mem.Allocator, line: []const u8) void {443    const parsed = std.json.parseFromSlice(std.json.Value, gpa, line, .{}) catch {444        output.logRow(line, "", "", ""); // unparseable — show raw rather than drop445        return;446    };447    defer parsed.deinit();448    if (parsed.value != .object) return;449    const o = parsed.value.object;450451    const ts = jsonStr(o, "ts");452    const date = if (ts.len >= 10) ts[0..10] else ts;453    const time = if (ts.len >= 16) ts[11..16] else "";454    const action = jsonStr(o, "command");455    const outcome = jsonStr(o, "outcome");456457    var buf: [160]u8 = undefined;458    var detail: []const u8 = outcome;459    if (o.get("gen_after")) |g| {460        if (g == .integer) {461            detail = std.fmt.bufPrint(&buf, "{s}   -> gen {d}", .{ outcome, g.integer }) catch outcome;462        }463    }464    output.logRow(date, time, action, detail);465}466467fn jsonStr(o: std.json.ObjectMap, key: []const u8) []const u8 {468    if (o.get(key)) |v| {469        if (v == .string) return v.string;470    }471    return "";472}473474pub fn edit(ctx: Ctx) !void {475    const hardware = ctx.args.len > 0 and std.mem.eql(u8, ctx.args[0], "hardware");476    const filename = if (hardware) "hardware-configuration.nix" else "configuration.nix";477    const path = try std.fmt.allocPrint(ctx.gpa, "{s}/{s}", .{ ctx.machine.config_path, filename });478    defer ctx.gpa.free(path);479    output.editOpen(path, ctx.cfg.editor);480    _ = try exec.openEditor(ctx.io, ctx.gpa, ctx.cfg.editor, path, null);481}482483pub fn fmt(ctx: Ctx) !void {484    const path = try std.fmt.allocPrint(ctx.gpa, "{s}/configuration.nix", .{ctx.machine.config_path});485    defer ctx.gpa.free(path);486    if (ctx.check) {487        const code = try exec.nixpkgsFmt(ctx.io, &ctx.machine, ctx.gpa, path, true);488        if (code != 0) output.fmtNeedsFormat() else output.fmtDone();489        return;490    }491    output.fmtStart(path);492    _ = try exec.nixpkgsFmt(ctx.io, &ctx.machine, ctx.gpa, path, false);493    output.fmtDone();494}495496pub fn repl(ctx: Ctx) !void {497    output.replStart();498    _ = try exec.nixRepl(ctx.io, &ctx.machine, ctx.gpa);499}500501pub fn develop(ctx: Ctx) !void {502    if (std.mem.eql(u8, ctx.sub, "show")) {503        _ = try exec.nixBuild(ctx.io, &ctx.machine, ctx.gpa, null);504        return;505    }506    const run_cmd: ?[]const u8 = if (ctx.args.len > 0 and std.mem.eql(u8, ctx.sub, "run"))507        ctx.args[0]508    else509        null;510    output.developStart(&.{});511    _ = try exec.nixDevelop(ctx.io, &ctx.machine, ctx.gpa, run_cmd);512    output.developDone();513}514515pub fn info(ctx: Ctx) !void {516    output.infoHeader(ctx.machine.name);517    const nixos_ver = exec.systemInfo(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");518    defer ctx.gpa.free(nixos_ver);519    output.infoRow("nixos", std.mem.trim(u8, nixos_ver, " \t\n\r"));520521    const kernel = exec.kernelInfo(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");522    defer ctx.gpa.free(kernel);523    output.infoRow("kernel", std.mem.trim(u8, kernel, " \t\n\r"));524525    const uptime = exec.uptimeInfo(ctx.gpa, ctx.io, &ctx.machine) catch try ctx.gpa.dupe(u8, "unknown");526    defer ctx.gpa.free(uptime);527    output.infoRow("uptime", std.mem.trim(u8, uptime, " \t\n\r"));528}529530pub fn boot(ctx: Ctx) !void {531    output.bootHeader(ctx.machine.name);532    const entries = exec.bootEntries(ctx.gpa, ctx.io, &ctx.machine) catch &.{};533    defer ctx.gpa.free(entries);534    for (entries, 0..) |entry, i| output.bootEntry(entry, i);535}536537pub fn searchCmd(ctx: Ctx) !void {538    const query = if (ctx.args.len > 0) ctx.args[0] else "";539    api.ensureRegistryPinned(ctx.gpa, ctx.io, ctx.environ);540    const selected = try search_tui.run(ctx.gpa, ctx.io, query, .packages);541    if (selected) |npkg| try selectAndInstall(ctx, npkg);542}543544pub fn optionCmd(ctx: Ctx) !void {545    const query = if (ctx.args.len > 0) ctx.args[0] else "";546    api.ensureRegistryPinned(ctx.gpa, ctx.io, ctx.environ);547    _ = try search_tui.run(ctx.gpa, ctx.io, query, .options);548}549550pub fn install(ctx: Ctx) !void {551    const pkg_name = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;552553    api.ensureRegistryPinned(ctx.gpa, ctx.io, ctx.environ);554555    // Search for the package to get the attr556    const pkgs = api.searchPackages(ctx.gpa, ctx.io, pkg_name) catch {557        errors.error_info.setMessage("'{s}' not found in nixpkgs", .{pkg_name});558        errors.error_info.setSuggestion("nina search {s}", .{pkg_name});559        return error.PackageNotFound;560    };561562    if (pkgs.len == 0) {563        // No matches — likely a typo. Suggest the closest real package and offer it.564        if (closestPackage(ctx, pkg_name)) |sugg| {565            output.didYouMean(pkg_name, sugg.pname);566            if (output.confirm("")) try selectAndInstall(ctx, sugg);567            return;568        }569        errors.error_info.setMessage("'{s}' not found in nixpkgs", .{pkg_name});570        errors.error_info.setSuggestion("nina search {s}", .{pkg_name});571        return error.PackageNotFound;572    }573574    // Prefer an exact name match over the first search result — `install ripgrep`575    // must pick "ripgrep", not the alphabetically-first match like "repgrep".576    var found = pkgs[0];577    for (pkgs) |p| {578        if (std.mem.eql(u8, p.pname, pkg_name)) {579            found = p;580            break;581        }582    }583    try selectAndInstall(ctx, found);584}585586// Find the real package whose name is closest to a not-found query (typo help).587// Typos usually keep the leading characters, so gather candidates with a prefix588// search and rank by edit distance; return the best only if it's typo-close.589fn closestPackage(ctx: Ctx, query: []const u8) ?types.NixPackage {590    if (query.len < 2) return null;591    var candidates = api.searchPackages(ctx.gpa, ctx.io, query[0..@min(query.len, 4)]) catch &.{};592    if (candidates.len == 0 and query.len > 2) {593        candidates = api.searchPackages(ctx.gpa, ctx.io, query[0..2]) catch &.{};594    }595    if (candidates.len == 0) return null;596597    var best: ?types.NixPackage = null;598    var best_dist: usize = std.math.maxInt(usize);599    for (candidates) |p| {600        const d = editDistance(query, p.pname);601        if (d > 0 and d < best_dist) {602            best_dist = d;603            best = p;604        }605    }606    // Only suggest a genuinely close name, scaled to query length.607    const threshold = @max(@as(usize, 2), query.len / 3);608    return if (best_dist <= threshold) best else null;609}610611// Case-insensitive Levenshtein distance. Names longer than the row buffer are612// treated as far away (never suggested).613fn editDistance(a: []const u8, b: []const u8) usize {614    const n = b.len;615    if (a.len == 0) return n;616    if (n == 0) return a.len;617    if (n + 1 > 128) return std.math.maxInt(usize);618    var prev: [128]usize = undefined;619    var curr: [128]usize = undefined;620    var j: usize = 0;621    while (j <= n) : (j += 1) prev[j] = j;622    var i: usize = 1;623    while (i <= a.len) : (i += 1) {624        curr[0] = i;625        j = 1;626        while (j <= n) : (j += 1) {627            const cost: usize = if (std.ascii.toLower(a[i - 1]) == std.ascii.toLower(b[j - 1])) 0 else 1;628            curr[j] = @min(prev[j] + 1, @min(curr[j - 1] + 1, prev[j - 1] + cost));629        }630        @memcpy(prev[0 .. n + 1], curr[0 .. n + 1]);631    }632    return prev[n];633}634635test "editDistance is case-insensitive Levenshtein" {636    try std.testing.expectEqual(@as(usize, 1), editDistance("firefux", "firefox"));637    try std.testing.expectEqual(@as(usize, 0), editDistance("git", "GIT"));638    try std.testing.expectEqual(@as(usize, 2), editDistance("fierfox", "firefox"));639    try std.testing.expectEqual(@as(usize, 7), editDistance("", "firefox"));640}641642// Prompt for the install method (profile / system / try) and dispatch. Shared by643// `nina install <pkg>` and the search widget's Enter-to-select — the letter keys644// are read here, after the search widget's raw mode is gone, so they don't645// collide with typing a query.646fn selectAndInstall(ctx: Ctx, npkg: types.NixPackage) !void {647    output.installSelectPrompt(npkg.pname, npkg.version);648649    var choice_buf: [8]u8 = undefined;650    var choice_len: usize = 0;651    while (choice_len < choice_buf.len) {652        const n = std.posix.read(std.posix.STDIN_FILENO, choice_buf[choice_len .. choice_len + 1]) catch break;653        if (n == 0) break;654        if (choice_buf[choice_len] == '\n') break;655        choice_len += 1;656    }657    const choice = std.mem.trim(u8, choice_buf[0..choice_len], " \t");658659    if (choice.len == 0 or choice[0] == 'i' or choice[0] == 'I') {660        try profileInstallPkg(ctx, npkg);661    } else if (choice[0] == 's' or choice[0] == 'S') {662        try systemInstallPkg(ctx, npkg);663    } else if (choice[0] == 't' or choice[0] == 'T') {664        try tryPkg(ctx, npkg);665    }666}667668fn profileInstallPkg(ctx: Ctx, npkg: types.NixPackage) !void {669    output.installStart(npkg.pname, .profile);670    output.printSubstep("nix profile add nixpkgs#{s}", .{npkg.attr});671    output.printTeach(try std.fmt.allocPrint(ctx.gpa, "nix profile add nixpkgs#{s}", .{npkg.attr}));672    const code = try exec.profileInstall(ctx.io, &ctx.machine, ctx.gpa, npkg.attr);673    if (code != 0) return error.ProcessFailed;674    output.installDone(npkg.pname);675}676677fn systemInstallPkg(ctx: Ctx, npkg: types.NixPackage) !void {678    const config_path = try std.fmt.allocPrint(ctx.gpa, "{s}/configuration.nix", .{ctx.machine.config_path});679    defer ctx.gpa.free(config_path);680    const line = api.findSystemPackagesLine(ctx.io, config_path) orelse 1;681    output.installEditorPrompt(npkg.pname, config_path, line, ctx.cfg.editor);682    _ = try exec.openEditor(ctx.io, ctx.gpa, ctx.cfg.editor, config_path, line);683    if (ctx.cfg.confirm) {684        if (output.confirm("apply now? ")) {685            try apply(ctx);686        }687    }688}689690fn tryPkg(ctx: Ctx, npkg: types.NixPackage) !void {691    output.tryStart(npkg.pname);692    output.printSubstep("nix shell nixpkgs#{s}", .{npkg.attr});693    _ = try exec.nixShell(ctx.io, &ctx.machine, ctx.gpa, npkg.attr);694    output.tryDone();695}696697pub fn remove(ctx: Ctx) !void {698    const pkg_name = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;699    output.removeStart(pkg_name);700    const code = try exec.profileRemove(ctx.io, &ctx.machine, ctx.gpa, pkg_name);701    if (code != 0) return error.ProcessFailed;702    output.done();703}704705pub fn list(ctx: Ctx) !void {706    output.listHeader(ctx.machine.name);707708    // Profile packages: parse `nix profile list --json` into clean name+version709    // rows instead of dumping nix's verbose multi-line "Name:/Flake attribute:"710    // blocks.711    if (exec.profileListJson(ctx.gpa, ctx.io, &ctx.machine) catch null) |json| {712        defer ctx.gpa.free(json);713        output.listSection("profile");714        renderProfileJson(ctx.gpa, json);715    }716717    // System packages: the references of /run/current-system/sw, each a718    // <hash>-<name>-<version> store path.719    if (exec.systemPackagePaths(ctx.gpa, ctx.io, &ctx.machine) catch null) |paths| {720        defer ctx.gpa.free(paths);721        output.blankLine();722        output.listSection("system");723        var lines = std.mem.splitScalar(u8, paths, '\n');724        while (lines.next()) |line| {725            const t = std.mem.trim(u8, line, " \t\r");726            if (t.len == 0) continue;727            const row = pkgFromStorePath(t);728            // Skip support paths with no parseable version (man pages, wrappers).729            if (row.version.len == 0) continue;730            output.listPkg(row.name, row.version);731        }732    }733}734735const PkgRow = struct { name: []const u8, version: []const u8 };736737// Split a nix store path basename (<hash>-<name>-<version>) into name + version.738// Version begins at the first '-' followed by a digit, which reliably lands on739// the name/version boundary since the 32-char hash and package names start with740// non-digits.741fn pkgFromStorePath(path: []const u8) PkgRow {742    const base = std.fs.path.basename(path);743    const after_hash = if (base.len > 33 and base[32] == '-') base[33..] else base;744    var i: usize = 0;745    while (i + 1 < after_hash.len) : (i += 1) {746        if (after_hash[i] == '-' and std.ascii.isDigit(after_hash[i + 1])) {747            return .{ .name = after_hash[0..i], .version = stripOutputSuffix(after_hash[i + 1 ..]) };748        }749    }750    return .{ .name = after_hash, .version = "" };751}752753// Multi-output derivations append the output name to the store path (e.g.754// curl-8.20.0-bin, less-692-man). Strip that suffix so the version column reads755// as just the version.756fn stripOutputSuffix(ver: []const u8) []const u8 {757    const suffixes = [_][]const u8{ "-bin", "-man", "-dev", "-doc", "-lib", "-info", "-out", "-static", "-devdoc" };758    for (suffixes) |s| {759        if (std.mem.endsWith(u8, ver, s)) return ver[0 .. ver.len - s.len];760    }761    return ver;762}763764test "pkgFromStorePath splits name and version at first dash-digit" {765    const a = pkgFromStorePath("/nix/store/abcdefghijklmnopqrstuvwxyz012345-ripgrep-15.1.0");766    try std.testing.expectEqualStrings("ripgrep", a.name);767    try std.testing.expectEqualStrings("15.1.0", a.version);768769    const b = pkgFromStorePath("/nix/store/abcdefghijklmnopqrstuvwxyz012345-helix-25.01");770    try std.testing.expectEqualStrings("helix", b.name);771    try std.testing.expectEqualStrings("25.01", b.version);772773    // multi-output store paths drop their output-name suffix774    const c = pkgFromStorePath("/nix/store/abcdefghijklmnopqrstuvwxyz012345-curl-8.20.0-bin");775    try std.testing.expectEqualStrings("curl", c.name);776    try std.testing.expectEqualStrings("8.20.0", c.version);777}778779fn firstStorePath(el: std.json.Value) ?[]const u8 {780    if (el != .object) return null;781    const sp = el.object.get("storePaths") orelse return null;782    if (sp != .array or sp.array.items.len == 0) return null;783    const first = sp.array.items[0];784    return if (first == .string) first.string else null;785}786787// `nix profile list --json` has two shapes across nix versions: newer keys each788// element by name (object), older lists them positionally (array). Handle both.789fn renderProfileJson(gpa: std.mem.Allocator, json: []const u8) void {790    const parsed = std.json.parseFromSlice(std.json.Value, gpa, json, .{}) catch return;791    defer parsed.deinit();792    if (parsed.value != .object) return;793    const elements = parsed.value.object.get("elements") orelse return;794    switch (elements) {795        .object => |obj| {796            var it = obj.iterator();797            while (it.next()) |entry| {798                const ver = if (firstStorePath(entry.value_ptr.*)) |p| pkgFromStorePath(p).version else "";799                output.listPkg(entry.key_ptr.*, ver);800            }801        },802        .array => |arr| {803            for (arr.items) |el| {804                const path = firstStorePath(el) orelse continue;805                const row = pkgFromStorePath(path);806                output.listPkg(row.name, row.version);807            }808        },809        else => {},810    }811}812813pub fn tryCmd(ctx: Ctx) !void {814    const pkg_name_arg = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;815    const shell_pkg = types.NixPackage{ .attr = pkg_name_arg, .pname = pkg_name_arg, .version = "", .description = "" };816    try tryPkg(ctx, shell_pkg);817}818819// --- Service commands ---820821pub fn service(ctx: Ctx) !void {822    if (std.mem.eql(u8, ctx.sub, "list") or ctx.sub.len == 0) {823        output.serviceListHeader(ctx.machine.name);824        const svcs = exec.serviceList(ctx.gpa, ctx.io, &ctx.machine) catch &.{};825        defer ctx.gpa.free(svcs);826        for (svcs) |s| output.serviceRow(s);827        return;828    }829    if (std.mem.eql(u8, ctx.sub, "logs")) {830        const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;831        const follow = ctx.args.len > 1 and std.mem.eql(u8, ctx.args[1], "-f");832        output.serviceLogsHeader(name, ctx.last, ctx.machine.name);833        _ = try exec.serviceLogs(ctx.io, &ctx.machine, ctx.gpa, name, ctx.last, follow);834        return;835    }836    if (std.mem.eql(u8, ctx.sub, "status")) {837        const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;838        output.serviceVerb("status", name, ctx.machine.name);839        _ = try exec.serviceCtl(ctx.io, &ctx.machine, ctx.gpa, "status", name);840        return;841    }842    // start/stop/restart/enable/disable843    const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;844    if (ctx.cfg.confirm and (std.mem.eql(u8, ctx.sub, "stop") or std.mem.eql(u8, ctx.sub, "disable"))) {845        if (!output.confirm("")) return;846    }847    output.serviceVerb(ctx.sub, name, ctx.machine.name);848    _ = try exec.serviceCtl(ctx.io, &ctx.machine, ctx.gpa, ctx.sub, name);849    output.serviceDone();850}851852// --- Flake commands ---853854pub fn flake(ctx: Ctx) !void {855    if (std.mem.eql(u8, ctx.sub, "update")) {856        output.flakeUpdateStart();857        const input: ?[]const u8 = if (ctx.args.len > 0) ctx.args[0] else null;858        _ = try exec.flakeUpdate(ctx.io, &ctx.machine, ctx.gpa, input);859        output.flakeLockWritten();860        return;861    }862    if (std.mem.eql(u8, ctx.sub, "check")) {863        output.flakeCheckStart();864        const code = try exec.flakeCheck(ctx.io, &ctx.machine, ctx.gpa);865        if (code == 0) output.flakeValid() else output.checkFailed("");866        return;867    }868    if (std.mem.eql(u8, ctx.sub, "show")) {869        const json_out = exec.flakeShow(ctx.gpa, ctx.io, &ctx.machine) catch null;870        if (json_out) |j| {871            defer ctx.gpa.free(j);872            output.flakeShowHeader("flake outputs");873            // Parse and display the flake outputs874            const parsed = std.json.parseFromSlice(std.json.Value, ctx.gpa, j, .{}) catch return;875            defer parsed.deinit();876            if (parsed.value == .object) {877                var it = parsed.value.object.iterator();878                while (it.next()) |entry| {879                    if (entry.value_ptr.* == .object) {880                        var inner = entry.value_ptr.object.iterator();881                        while (inner.next()) |e2| {882                            const desc = if (e2.value_ptr.* == .object)883                                if (e2.value_ptr.object.get("description")) |d|884                                    if (d == .string) d.string else ""885                                else ""886                            else "";887                            output.flakeShowEntry(entry.key_ptr.*, e2.key_ptr.*, desc);888                        }889                    }890                }891            }892        }893        return;894    }895    if (std.mem.eql(u8, ctx.sub, "init")) {896        _ = try exec.flakeInit(ctx.io, &ctx.machine, ctx.gpa);897        return;898    }899    if (std.mem.eql(u8, ctx.sub, "lock")) {900        _ = try exec.flakeLock(ctx.io, &ctx.machine, ctx.gpa);901        return;902    }903    if (std.mem.eql(u8, ctx.sub, "clone")) {904        const url = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;905        _ = try exec.flakeClone(ctx.io, &ctx.machine, ctx.gpa, url);906        return;907    }908}909910// --- Store commands ---911912pub fn store(ctx: Ctx) !void {913    if (std.mem.eql(u8, ctx.sub, "gc")) {914        _ = try exec.storeGc(ctx.io, &ctx.machine, ctx.gpa);915        return;916    }917    if (std.mem.eql(u8, ctx.sub, "verify")) {918        output.storeVerifyStart();919        _ = try exec.storeVerify(ctx.io, &ctx.machine, ctx.gpa);920        return;921    }922    if (std.mem.eql(u8, ctx.sub, "repair")) {923        if (ctx.cfg.confirm and !output.confirm("")) return;924        output.storeRepairStart();925        _ = try exec.storeRepair(ctx.io, &ctx.machine, ctx.gpa);926        return;927    }928    if (std.mem.eql(u8, ctx.sub, "info")) {929        const si = try exec.storeInfo(ctx.gpa, ctx.io, &ctx.machine);930        output.storeInfo(si);931        return;932    }933    if (std.mem.eql(u8, ctx.sub, "path")) {934        const attr = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;935        const path = try exec.storePath(ctx.gpa, ctx.io, &ctx.machine, attr);936        defer ctx.gpa.free(path);937        output.storePath(path);938        return;939    }940    // default: info941    const si = try exec.storeInfo(ctx.gpa, ctx.io, &ctx.machine);942    output.storeInfo(si);943}944945// --- Channel commands ---946947pub fn channel(ctx: Ctx) !void {948    if (std.mem.eql(u8, ctx.sub, "list") or ctx.sub.len == 0) {949        output.channelListHeader(ctx.machine.name);950        const chans = try exec.channelList(ctx.gpa, ctx.io, &ctx.machine);951        defer ctx.gpa.free(chans);952        for (chans) |ch| output.channelRow(ch);953        return;954    }955    if (std.mem.eql(u8, ctx.sub, "add")) {956        const url = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;957        const name = if (ctx.args.len > 1) ctx.args[1] else "";958        _ = try exec.channelAdd(ctx.io, &ctx.machine, ctx.gpa, url, name);959        output.channelDone();960        return;961    }962    if (std.mem.eql(u8, ctx.sub, "remove")) {963        const name = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;964        _ = try exec.channelRemove(ctx.io, &ctx.machine, ctx.gpa, name);965        output.channelDone();966        return;967    }968    if (std.mem.eql(u8, ctx.sub, "update")) {969        return channelUpdateCmd(ctx);970    }971}972973// --- Profile commands ---974975pub fn profile(ctx: Ctx) !void {976    if (std.mem.eql(u8, ctx.sub, "list") or ctx.sub.len == 0) {977        output.profileListHeader();978        const out = exec.profileList(ctx.gpa, ctx.io, &ctx.machine) catch null;979        if (out) |o| {980            defer ctx.gpa.free(o);981            var lines = std.mem.splitScalar(u8, o, '\n');982            while (lines.next()) |line| {983                const t = std.mem.trim(u8, line, " \t\r");984                if (t.len == 0) continue;985                output.profileRow(t, "", "");986            }987        }988        return;989    }990    if (std.mem.eql(u8, ctx.sub, "install")) {991        const attr = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;992        _ = try exec.profileInstall(ctx.io, &ctx.machine, ctx.gpa, attr);993        output.done();994        return;995    }996    if (std.mem.eql(u8, ctx.sub, "remove")) {997        const attr = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;998        _ = try exec.profileRemove(ctx.io, &ctx.machine, ctx.gpa, attr);999        output.done();1000        return;1001    }1002    if (std.mem.eql(u8, ctx.sub, "upgrade")) {1003        _ = try exec.profileUpgrade(ctx.io, &ctx.machine, ctx.gpa);1004        output.done();1005        return;1006    }1007}10081009// --- Pkg commands ---10101011pub fn pkg(ctx: Ctx) !void {1012    const name = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;1013    if (std.mem.eql(u8, ctx.sub, "why")) {1014        _ = try exec.pkgWhy(ctx.io, &ctx.machine, ctx.gpa, name);1015        return;1016    }1017    if (std.mem.eql(u8, ctx.sub, "deps")) {1018        _ = try exec.pkgDeps(ctx.io, &ctx.machine, ctx.gpa, name);1019        return;1020    }1021    if (std.mem.eql(u8, ctx.sub, "size")) {1022        const s = exec.pkgSize(ctx.gpa, ctx.io, &ctx.machine, name) catch null;1023        if (s) |sz| {1024            defer ctx.gpa.free(sz);1025            output.pkgResult("size", sz);1026        }1027        return;1028    }1029    if (std.mem.eql(u8, ctx.sub, "path")) {1030        const p = try exec.storePath(ctx.gpa, ctx.io, &ctx.machine, name);1031        defer ctx.gpa.free(p);1032        output.pkgResult("path", p);1033        return;1034    }1035    if (std.mem.eql(u8, ctx.sub, "closure")) {1036        _ = try exec.pkgClosure(ctx.io, &ctx.machine, ctx.gpa, name);1037        return;1038    }1039}10401041// --- Pin / Unpin ---10421043pub fn pin(ctx: Ctx) !void {1044    const input = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;1045    const commit = if (ctx.args.len > 1) ctx.args[1] else return error.Internal;1046    exec.editFlakeLock(ctx.gpa, input, commit) catch return error.ProcessFailed;1047    output.pinDone(input, commit);1048}10491050pub fn unpin(ctx: Ctx) !void {1051    const input = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;1052    _ = try exec.flakeUpdate(ctx.io, &ctx.machine, ctx.gpa, input);1053    output.unpinDone(input);1054}10551056// --- Hash / Fetch ---10571058pub fn hash(ctx: Ctx) !void {1059    const path = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;1060    const h = try exec.nixHash(ctx.gpa, ctx.io, &ctx.machine, path);1061    defer ctx.gpa.free(h);1062    output.hashResult(std.mem.trim(u8, h, " \t\n\r"));1063}10641065pub fn fetch(ctx: Ctx) !void {1066    const url = if (ctx.args.len > 0) ctx.args[0] else return error.Internal;1067    const h = try exec.nixFetch(ctx.gpa, ctx.io, &ctx.machine, url);1068    defer ctx.gpa.free(h);1069    output.fetchResult(std.mem.trim(u8, h, " \t\n\r"), url);1070}10711072// --- Build / Run ---10731074pub fn build(ctx: Ctx) !void {1075    const attr: ?[]const u8 = if (ctx.args.len > 0) ctx.args[0] else null;1076    output.buildStart(attr orelse "default");1077    const code = try exec.nixBuild(ctx.io, &ctx.machine, ctx.gpa, attr);1078    if (code != 0) return error.BuildFailed;1079    output.buildDone();1080}10811082pub fn runCmd(ctx: Ctx) !void {1083    const pkg_arg = if (ctx.args.len > 0) ctx.args[0] else return error.PackageNotFound;1084    output.runStart(pkg_arg);1085    _ = try exec.nixRun(ctx.io, &ctx.machine, ctx.gpa, pkg_arg);1086}10871088// --- Hello ---10891090pub fn hello(ctx: Ctx) !void {1091    output.hello(ctx.cfg.machines);1092}10931094pub fn setup(gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map) !void {1095    output.setupCheck();1096    const status = exec.checkNixFeatures(gpa, io, environ);1097    output.setupFeatures(status.nix_command, status.flakes);10981099    if (status.allEnabled()) {1100        output.setupAllSet();1101        api.ensureRegistryPinned(gpa, io, environ);1102        return;1103    }11041105    output.setupEnabling();1106    const path = try exec.enableNixFeatures(gpa, io, environ);1107    defer gpa.free(path);11081109    output.setupDone(path, exec.isNixOS(io));1110    api.ensureRegistryPinned(gpa, io, environ);1111}11121113// --- Log helper ---11141115fn logAction(ctx: Ctx, action: []const u8, gen: u32) void {1116    var ts_buf: [32]u8 = undefined;1117    const ts = nowIso(ctx.io, &ts_buf);1118    var buf: [256]u8 = undefined;1119    const entry = std.fmt.bufPrint(&buf, "{{\"ts\":\"{s}\",\"machine\":\"{s}\",\"command\":\"{s}\",\"outcome\":\"success\",\"gen_before\":null,\"gen_after\":{d},\"duration_ms\":0}}\n", .{1120        ts, ctx.machine.name, action, gen,1121    }) catch return;1122    exec.appendNinaLog(ctx.io, ctx.environ, entry);1123}11241125// Wall-clock UTC as ISO-8601. Uses Io.Clock.real (the settable Unix-epoch clock);1126// Io.Clock.awake is monotonic and unsuitable for timestamps. Falls back to epoch 0.1127fn nowIso(io: std.Io, buf: []u8) []const u8 {1128    const fallback = "1970-01-01T00:00:00Z";1129    const ns = std.Io.Clock.real.now(io).toNanoseconds();1130    if (ns <= 0) return fallback;1131    const secs: u64 = @intCast(@divFloor(ns, 1_000_000_000));1132    const es = std.time.epoch.EpochSeconds{ .secs = secs };1133    const ed = es.getEpochDay();1134    const yd = ed.calculateYearDay();1135    const md = yd.calculateMonthDay();1136    const ds = es.getDaySeconds();1137    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", .{1138        yd.year, md.month.numeric(), @as(u32, md.day_index) + 1,1139        ds.getHoursIntoDay(), ds.getMinutesIntoHour(), ds.getSecondsIntoMinute(),1140    }) catch return fallback;1141}