Compare commits

..

No commits in common. "887fe2f49d2e09ec90ea48d970961fa5495105cb" and "8f51affe7c7c86c6560a473998884b8438465258" have entirely different histories.

5 changed files with 43 additions and 136 deletions

View file

@ -11,19 +11,10 @@ var dryCmd = &cobra.Command{
Short: "poor man's nix flake check",
Run: func(cmd *cobra.Command, args []string) {
oizys.CheckFlake(flake)
oizys.NixDryRun(flake, host, verbose)
oizys.NixDryRun(flake, host)
},
}
var verbose bool
func init() {
rootCmd.AddCommand(dryCmd)
dryCmd.Flags().BoolVarP(
&verbose,
"verbose",
"v",
false,
"show verbose output",
)
}

View file

@ -29,12 +29,11 @@ func TerminalSize() (int, int) {
return w, h
}
func ParseDryRunOutput(nixOutput string, verbose bool) {
func ParseDryRunOutput(nixOutput string) {
output := termenv.NewOutput(os.Stdout)
parts := strings.Split(nixOutput, "\nthese")
if len(parts) != 3 {
log.Println("no changes...")
log.Println(output)
return
}
built := strings.Split(strings.TrimSpace(parts[1]), "\n")[1:]
@ -59,18 +58,15 @@ func ParseDryRunOutput(nixOutput string, verbose bool) {
rows = append(rows, table.Row{hash, name})
}
if verbose {
w, _ := TerminalSize()
columns := []table.Column{
{Title: "hash", Width: 34},
{Title: "pkg", Width: int(w / 4)},
}
ShowTable(columns, rows)
}
}
func NixDryRun(flake string, host string, verbose bool) {
func NixDryRun(flake string, host string) {
output := termenv.NewOutput(os.Stdout)
path := Output(flake, host)
cmd := exec.Command("nix", "build", path, "--dry-run")
@ -89,7 +85,7 @@ func NixDryRun(flake string, host string, verbose bool) {
fmt.Println(string(result))
log.Fatal(err)
}
ParseDryRunOutput(string(result), verbose)
ParseDryRunOutput(string(result))
}
func NixosRebuild(subcmd string, flake string, rest ...string) {

View file

@ -34,6 +34,7 @@ pub fn init(allocator: Allocator) !Cli {
try subcmd.addArg(Arg.positional("forward", null, null));
try subcmd.addArg(Arg.singleValueOption("flake", 'f', "path to flake"));
try subcmd.addArg(Arg.singleValueOption("host", null, "hostname (default: current host)"));
try subcmd.addArg(Arg.booleanOption("no-pinix", null, "don't use pinix"));
try oizys.addSubcommand(subcmd.*);
}

View file

@ -9,6 +9,7 @@ host: []const u8,
cache_name: []const u8,
output: []const u8,
cmd: OizysCmd,
no_pinix: bool,
debug: bool = false,
forward: ?[][]const u8,
@ -24,52 +25,39 @@ pub const OizysCmd = enum {
pub fn init(allocator: std.mem.Allocator, matches: *const ArgMatches, forward: ?[][]const u8) !Oizys {
const cmd = matches.subcommand.?.name;
const flags = matches.subcommandMatches(cmd).?;
var oizys = Oizys{
.allocator = allocator,
.host = undefined,
.flake = undefined,
.output = undefined,
.cmd = std.meta.stringToEnum(OizysCmd, cmd).?,
.cache_name = flags.getSingleValue("cache") orelse "daylin",
.forward = forward,
};
if (flags.getSingleValue("host")) |host| {
oizys.host = try allocator.dupe(u8, host);
} else {
oizys.host = try Oizys.getDefaultHostName(allocator);
}
if (flags.getSingleValue("flake")) |flake| {
oizys.flake = try allocator.dupe(u8, flake);
} else {
oizys.flake = try Oizys.getDefaultFlake(allocator);
}
const host = flags.getSingleValue("host") orelse
try Oizys.getDefaultHostName(allocator);
const flake = flags.getSingleValue("flake") orelse
try Oizys.getDefaultFlake(allocator);
oizys.output = try std.fmt.allocPrint(
return Oizys{
.allocator = allocator,
.host = host,
.flake = flake,
.output = try std.fmt.allocPrint(
allocator,
"{s}#nixosConfigurations.{s}.config.system.build.toplevel",
.{ oizys.flake, oizys.host },
);
return oizys;
// return Oizys{
// .allocator = allocator,
// .host = host,
// .flake = flake,
// .output = try std.fmt.allocPrint(
// allocator,
// "{s}#nixosConfigurations.{s}.config.system.build.toplevel",
// .{ flake, host },
// ),
// .cmd = std.meta.stringToEnum(OizysCmd, cmd).?,
// .cache_name = flags.getSingleValue("cache") orelse "daylin",
// .forward = forward,
// };
.{ flake, host },
),
.cmd = std.meta.stringToEnum(OizysCmd, cmd).?,
.cache_name = flags.getSingleValue("cache") orelse "daylin",
.no_pinix = flags.containsArg("no-pinix"),
.forward = forward,
};
}
pub fn deinit(self: *Oizys) void {
self.allocator.free(self.flake);
self.allocator.free(self.output);
self.allocator.free(self.host);
self.allocator.free(self.output);
}
pub fn nix(self: *Oizys) []const u8 {
return if (self.no_pinix) "nix" else "pix";
}
pub fn nixos_rebuild(self: *Oizys) []const u8 {
return if (self.no_pinix) "nixos-rebuild" else "pixos-rebuild";
}
pub fn getDefaultHostName(allocator: Allocator) ![]const u8 {
@ -101,8 +89,8 @@ pub fn runNixCmd(self: *Oizys, cmd: NixCmd, argv: []const []const u8) !void {
defer args.deinit();
switch (cmd) {
NixCmd.Nix => try args.append("nix"),
NixCmd.NixosRebuild => try args.appendSlice(&.{ "sudo", "nixos-rebuild" }),
NixCmd.Nix => try args.append(self.nix()),
NixCmd.NixosRebuild => try args.appendSlice(&.{ "sudo", self.nixos_rebuild() }),
}
try args.appendSlice(argv);
if (self.forward) |fwd| try args.appendSlice(fwd);
@ -129,77 +117,12 @@ pub fn cache(self: *Oizys) !void {
_ = try p.spawnAndWait();
}
const DryResult = struct {
allocator: Allocator,
fetch: [][]const u8,
build: [][]const u8,
pub fn parse(allocator: Allocator, output: []const u8) !DryResult {
var it = std.mem.splitSequence(u8, output, ":\n");
_ = it.next();
var fetch = std.ArrayList([]const u8).init(allocator);
var build = std.ArrayList([]const u8).init(allocator);
if (it.next()) |x| {
try parseLines(x, &fetch);
} else {
return error.DryParseError;
}
if (it.next()) |x| {
try parseLines(x, &build);
} else {
return error.DryParseError;
}
return .{
.allocator = allocator,
.fetch = try fetch.toOwnedSlice(),
.build = try build.toOwnedSlice(),
};
}
pub fn deinit(self: *DryResult) void {
self.allocator.free(self.fetch);
self.allocator.free(self.build);
// for (self.fetch) |item| {
// self.allocator.free(item);
// }
// for (self.build) |item| {
// self.allocator.free(item);
// }
}
fn parseLines(buffer: []const u8, list: *std.ArrayList([]const u8)) !void {
var lines = std.mem.splitSequence(u8, buffer, "\n");
while (lines.next()) |line| {
try list.append(line);
}
}
};
pub fn dry(self: *Oizys) !void {
const cmd_output = try std.ChildProcess.run(.{
.allocator = self.allocator,
.argv = &.{ "nix", "build", self.output, "--dry-run" },
});
defer self.allocator.free(cmd_output.stdout);
defer self.allocator.free(cmd_output.stderr);
var result = try DryResult.parse(self.allocator, cmd_output.stderr);
defer result.deinit();
std.debug.print(
"to fetch: {d}\nto build: {d}\n",
.{ result.fetch.len, result.build.len },
);
}
pub fn run(self: *Oizys) !void {
switch (self.cmd) {
.@"switch" => try self.runNixCmd(.NixosRebuild, &.{ "switch", "--flake", self.flake }),
.boot => try self.runNixCmd(.NixosRebuild, &.{ "boot", "--flake", self.flake }),
.dry => try self.dry(),
.build => try self.runNixCmd(.Nix, &.{ "build", self.output }),
.@"switch" => try self.runNixCmd(NixCmd.NixosRebuild, &.{ "switch", "--flake", self.flake }),
.boot => try self.runNixCmd(NixCmd.NixosRebuild, &.{ "boot", "--flake", self.flake }),
.dry => try self.runNixCmd(NixCmd.Nix, &.{ "build", self.output, "--dry-run" }),
.build => try self.runNixCmd(NixCmd.Nix, &.{ "build", self.output }),
.output => {
const stdout = std.io.getStdOut().writer();
try stdout.print("{s}\n", .{self.output});

View file

@ -4,13 +4,9 @@ const Cli = @import("Cli.zig");
pub fn main() !void {
// memory management isn't hard :P
// var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
// defer arena.deinit();
// const allocator = arena.allocator();
//
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var cli = try Cli.init(allocator);
try cli.parse();