From e78e4a6fd180067f65aa953e648e5858010cd379 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Fri, 26 Jan 2024 15:27:13 -0600 Subject: [PATCH] styx.sh -> styx.nim --- lib/default.nix | 5 +- overlays/nimble/default.nix | 2 +- styx/default.nix | 11 +++ styx/lock.json | 1 + styx/styx.nim | 136 ++++++++++++++++++++++++++++++++++++ styx/styx.nimble | 14 ++++ 6 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 styx/default.nix create mode 100644 styx/lock.json create mode 100644 styx/styx.nim create mode 100644 styx/styx.nimble diff --git a/lib/default.nix b/lib/default.nix index 13b3b22..057cc98 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -14,10 +14,9 @@ in rec { buildStyx = _: forAllSystems ( pkgs: let - name = baseNameOf ../styx; - pkg = pkgs.writeScriptBin name (readFile ../styx/styx.sh); + pkg = pkgs.callPackage ../styx {}; in { - ${name} = pkg; + styx = pkg; default = pkg; } ); diff --git a/overlays/nimble/default.nix b/overlays/nimble/default.nix index bd90a07..a7a6391 100644 --- a/overlays/nimble/default.nix +++ b/overlays/nimble/default.nix @@ -2,7 +2,7 @@ nimble = prev.nimble.overrideNimAttrs { version = "0.14.2-f74bf2"; requiredNimVersion = 2; - buildInputs = [ prev.pkgs.openssl ]; + buildInputs = [prev.pkgs.openssl]; src = prev.fetchFromGitHub { owner = "nim-lang"; diff --git a/styx/default.nix b/styx/default.nix new file mode 100644 index 0000000..b4549b3 --- /dev/null +++ b/styx/default.nix @@ -0,0 +1,11 @@ +{ + lib, + buildNimPackage, + fetchFromGitHub, +}: +buildNimPackage (final: prev: { + pname = "styx"; + version = "2023.1001"; + src = ./.; + lockFile = ./lock.json; +}) diff --git a/styx/lock.json b/styx/lock.json new file mode 100644 index 0000000..f2a5012 --- /dev/null +++ b/styx/lock.json @@ -0,0 +1 @@ +{"depends":[{"method":"fetchzip","packages":["cligen"],"path":"/nix/store/b5dgqx2wds42kw2241qv7rmdf29hwl20-source","ref":"1.6.18","rev":"c54e2f3ea09d405a0d1b9bd8e7c0472d657d1193","sha256":"0ms572f6563bc2cr65gnh943adld5xbd6jy2w7gwvybxpczkxlf3","srcDir":"","url":"https://github.com/c-blake/cligen/archive/c54e2f3ea09d405a0d1b9bd8e7c0472d657d1193.tar.gz"}]} diff --git a/styx/styx.nim b/styx/styx.nim new file mode 100644 index 0000000..c68b405 --- /dev/null +++ b/styx/styx.nim @@ -0,0 +1,136 @@ +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, "swith" + + +# proc matchCmd(cmd: string): proc = +# case cmd: +# of "dry": dry +# of "switch": switch +# of "boot": boot +# of "cache": cache +# of "build": build +# else: +# error "unknown command", cmd +# quit 1 +# + +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 + quit 1 + + +const usage = """ +styx [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 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 + + info $c + runCmd c, cmd diff --git a/styx/styx.nimble b/styx/styx.nimble new file mode 100644 index 0000000..decc7b9 --- /dev/null +++ b/styx/styx.nimble @@ -0,0 +1,14 @@ +# Package + +author = "Daylin Morgan" +version = "2023.1001" +description = "styx" +license = "MIT" +srcDir = "." +bin = @["styx"] + + +# Dependencies + +requires "nim >= 2.0.0" +requires "cligen"