From 8975830af783be1d851ec6d2c330f88a2b841e7f Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sun, 28 Jan 2024 11:00:05 -0600 Subject: [PATCH] styx is dead long live oizys --- flake.nix | 6 +-- lib/default.nix | 6 +-- modules/nix.nix | 2 +- oizys/default.nix | 9 ++++ oizys/oizys.nim | 127 +++++++++++++++++++++++++++++++++++++++++++++ oizys/oizys.nimble | 13 +++++ 6 files changed, 156 insertions(+), 7 deletions(-) create mode 100644 oizys/default.nix create mode 100644 oizys/oizys.nim create mode 100644 oizys/oizys.nimble diff --git a/flake.nix b/flake.nix index 60b776e..dd2a35a 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "daylinmorgan-nixcfg"; + description = "nix begat oizys"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; @@ -35,10 +35,10 @@ ... }: let lib = import ./lib {inherit inputs nixpkgs;}; - inherit (lib) findModules mapHosts buildStyx; + inherit (lib) findModules mapHosts buildOizys; in { nixosModules = findModules ./modules; nixosConfigurations = mapHosts ./hosts; - packages = buildStyx {}; + packages = buildOizys {}; }; } diff --git a/lib/default.nix b/lib/default.nix index 057cc98..49efcc2 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -11,12 +11,12 @@ in rec { forAllSystems = f: genAttrs supportedSystems (system: f nixpkgs.legacyPackages.${system}); - buildStyx = _: + buildOizys = _: forAllSystems ( pkgs: let - pkg = pkgs.callPackage ../styx {}; + pkg = pkgs.callPackage ../oizys {}; in { - styx = pkg; + oizys = pkg; default = pkg; } ); diff --git a/modules/nix.nix b/modules/nix.nix index 84c5105..03152f8 100644 --- a/modules/nix.nix +++ b/modules/nix.nix @@ -19,7 +19,7 @@ environment.systemPackages = with pkgs; [ nix-output-monitor alejandra - inputs.self.packages.${pkgs.system}.styx + inputs.self.packages.${pkgs.system}.oizys ]; nix.settings = { diff --git a/oizys/default.nix b/oizys/default.nix new file mode 100644 index 0000000..bcf7680 --- /dev/null +++ b/oizys/default.nix @@ -0,0 +1,9 @@ +{ + lib, + buildNimPackage, +}: +buildNimPackage (final: prev: { + pname = "oizys"; + version = "unstable"; + src = ./.; +}) diff --git a/oizys/oizys.nim b/oizys/oizys.nim new file mode 100644 index 0000000..daa888c --- /dev/null +++ b/oizys/oizys.nim @@ -0,0 +1,127 @@ +import std/[logging, os, osproc, tables, times] +from std/nativesockets import getHostname + + +var logger = newConsoleLogger() +addHandler(logger) +let summaryFile = getEnv("GITHUB_STEP_SUMMARY") + +type + StyxContext = object + flake, host: string + cache = "daylin" + nom: bool = true + +proc newCtx(): StyxContext = + result = StyxContext() + result.flake = getEnv("FLAKE_PATH", getEnv("HOME") / "nixcfg") + result.host = getHostname() + +proc systemFlakePath(c: StyxContext): string = + c.flake & "#nixosConfigurations." & c.host & ".config.system.build.toplevel" + +proc execQuit(cmd: string) = + quit (execCmd cmd) + +proc build(c: StyxContext) = + ## build nixos + let + cmd = if c.nom: "nom" else: "nix" + execQuit cmd & " build " & c.systemFlakePath + +proc dry(c: StyxContext) = + ## poor man's nix flake check + execQuit "nix build " & c.systemFlakePath & " --dry-run" + +proc cache(c: StyxContext) = + # Simple benchmarking + let start = cpuTime() + let code = execCmd """ + cachix watch-exec """ & c.cache & """ \ + -- \ + nix build """ & c.systemFlakePath & """ \ + --print-build-logs \ + --accept-flake-config + """ + let duration = (cpuTime() - start) + if code != 0: + error "faile to build configuration for: ", c.host + + if summaryFile != "": + writeFile( + summaryFile, + "Built host: " & c.host & " in " & $duration & " seconds" + ) + info "Built host: " & c.host & " in " & $duration & " seconds" + + +proc nixosRebuild(c: StyxContext, cmd: string) = + execQuit "sudo nixos-rebuild " & cmd & " " & " --flake " & c.flake + +proc boot(c: StyxContext) = + ## nixos rebuild boot + nixosRebuild c, "build" + +proc switch(c: StyxContext) = + ## nixos rebuild switch + nixosRebuild c, "switch" + +const usage = """ +oizys [opts] + commands: + dry poor man's nix flake check + boot nixos-rebuild boot + switch nixos-rebuild switch + cache build and push to cachix + build build system flake + + options: + --help > show this help + -h|--host > hostname (current host) + -f|--flake > path to flake ($FLAKE_PATH or $HOME/styx) + -c|--cache > name of cachix binary cache (daylin) +""" + +proc runCmd(c: StyxContext, cmd: string) = + case cmd: + of "dry": dry c + of "switch": switch c + of "boot": boot c + of "cache": cache c + of "build": build c + else: + error "unknown command: ", cmd + echo usage + quit 1 + + +proc parseFlag(c: var StyxContext, key, val: string) = + case key: + of "help": + echo usage; quit 0 + of "h","host": + c.host = val + of "f","flake": + c.flake = val + of "no-nom": + c.nom = false + +when isMainModule: + import std/parseopt + var + c = newCtx() + cmd: string + for kind, key, val in getopt(): + case kind + of cmdArgument: + cmd = key + of cmdLongOption, cmdShortOption: + parseFlag c, key, val + of cmdEnd: + discard + if cmd == "": + echo "please specify a command" + echo usage; quit 1 + + info $c + runCmd c, cmd diff --git a/oizys/oizys.nimble b/oizys/oizys.nimble new file mode 100644 index 0000000..1e1d304 --- /dev/null +++ b/oizys/oizys.nimble @@ -0,0 +1,13 @@ +# Package + +author = "Daylin Morgan" +version = "0.1.0" +description = "oizys" +license = "MIT" +srcDir = "." +bin = @["oizys"] + + +# Dependencies + +requires "nim >= 2.0.0"