mirror of
https://github.com/daylinmorgan/oizys.git
synced 2025-02-22 23:35:51 -06:00
fewer nix calls yahoo
This commit is contained in:
parent
5edc904117
commit
6afbbc6bdd
2 changed files with 50 additions and 110 deletions
|
@ -144,11 +144,13 @@ hwylCli:
|
||||||
if minimal and system:
|
if minimal and system:
|
||||||
echo "--minimal and --system are mutually exclusive"
|
echo "--minimal and --system are mutually exclusive"
|
||||||
elif minimal:
|
elif minimal:
|
||||||
showOizysDerivations()
|
echo missingDerivations().fmtDrvsForNix()
|
||||||
elif system:
|
|
||||||
echo getSystemPaths().mapIt(it & "^*").join(" ")
|
|
||||||
else:
|
else:
|
||||||
echo nixosConfigAttrs().join(" ")
|
echo nixosAttrs(
|
||||||
|
if system: "path"
|
||||||
|
else: "build.toplevel"
|
||||||
|
).join(" ")
|
||||||
|
|
||||||
[update]
|
[update]
|
||||||
... "update and run nixos-rebuild"
|
... "update and run nixos-rebuild"
|
||||||
flags:
|
flags:
|
||||||
|
|
|
@ -1,22 +1,13 @@
|
||||||
import std/[
|
import std/[
|
||||||
algorithm, json,
|
algorithm, json, enumerate,
|
||||||
enumerate, os, sequtils, sets, strformat,
|
os, sequtils, strformat, strutils,
|
||||||
strutils, sugar, logging, tables, times
|
sugar, logging, tables, times
|
||||||
]
|
]
|
||||||
export tables
|
export tables
|
||||||
import hwylterm, hwylterm/logging, jsony
|
import hwylterm, hwylterm/logging, jsony
|
||||||
|
|
||||||
import ./[context, exec]
|
import ./[context, exec]
|
||||||
|
|
||||||
|
|
||||||
# TODO: autogenerate/deduplicate from modules/essentials.nix?
|
|
||||||
# const subFlags = [
|
|
||||||
# "--extra-substituters",
|
|
||||||
# "https://nix-cache.dayl.in",
|
|
||||||
# "--extra-trusted-public-keys",
|
|
||||||
# "nix-cache.dayl.in-1:lj22Sov7m1snupBz/43O1fxyEfy/S7cxBpweD7iREcs="
|
|
||||||
# ]
|
|
||||||
|
|
||||||
type
|
type
|
||||||
Substituters = object
|
Substituters = object
|
||||||
`extra-substituters`: seq[string]
|
`extra-substituters`: seq[string]
|
||||||
|
@ -29,7 +20,6 @@ func makeSubFlags(): seq[string] =
|
||||||
result.add "--" & k
|
result.add "--" & k
|
||||||
result.add "\"" & v.join(" ") & "\""
|
result.add "\"" & v.join(" ") & "\""
|
||||||
|
|
||||||
|
|
||||||
const subFlags = makeSubFlags()
|
const subFlags = makeSubFlags()
|
||||||
|
|
||||||
proc nixCommand*(cmd: string, nom: bool = false): string =
|
proc nixCommand*(cmd: string, nom: bool = false): string =
|
||||||
|
@ -47,12 +37,14 @@ proc nixCommand*(cmd: string, nom: bool = false): string =
|
||||||
if isBootstrap():
|
if isBootstrap():
|
||||||
result.addArgs subFlags
|
result.addArgs subFlags
|
||||||
|
|
||||||
proc nixosConfigAttr(host: string): string =
|
proc nixosAttr(host: string, attr: string = "build.toplevel"): string =
|
||||||
getFlake() & "#nixosConfigurations." & host & ".config.system.build.toplevel"
|
getFlake() & "#nixosConfigurations." & host & ".config.system." & attr
|
||||||
|
|
||||||
proc nixosConfigAttrs*(): seq[string] =
|
proc nixosAttrs*(
|
||||||
|
attr: string = "build.toplevel"
|
||||||
|
): seq[string] =
|
||||||
for host in getHosts():
|
for host in getHosts():
|
||||||
result.add nixosConfigAttr(host)
|
result.add nixosAttr(host, attr)
|
||||||
|
|
||||||
type
|
type
|
||||||
NixosRebuildSubcmd* = enum
|
NixosRebuildSubcmd* = enum
|
||||||
|
@ -158,7 +150,7 @@ proc display(output: DryRunOutput) =
|
||||||
proc toBuildNixosConfiguration(): seq[string] =
|
proc toBuildNixosConfiguration(): seq[string] =
|
||||||
var cmd = nixCommand("build")
|
var cmd = nixCommand("build")
|
||||||
cmd.addArg "--dry-run"
|
cmd.addArg "--dry-run"
|
||||||
cmd.addArgs nixosConfigAttrs()
|
cmd.addArgs nixosAttrs()
|
||||||
let (_, err) = runCmdCaptWithSpinner(
|
let (_, err) = runCmdCaptWithSpinner(
|
||||||
cmd,
|
cmd,
|
||||||
"running dry run build for: " & (getHosts().join(" ").bb("bold")),
|
"running dry run build for: " & (getHosts().join(" ").bb("bold")),
|
||||||
|
@ -185,12 +177,12 @@ proc narHash*(s: string): string =
|
||||||
let ss = s.split("-")
|
let ss = s.split("-")
|
||||||
result = ss[0].split("/")[^1]
|
result = ss[0].split("/")[^1]
|
||||||
|
|
||||||
proc evaluateDerivations(drvs: openArray[string]): Table[string, NixDerivation] =
|
# proc evaluateDerivations(drvs: openArray[string]): Table[string, NixDerivation] =
|
||||||
var cmd = "nix derivation show -r"
|
# var cmd = "nix derivation show -r"
|
||||||
cmd.addArgs drvs
|
# cmd.addArgs drvs
|
||||||
let (output, _) =
|
# let (output, _) =
|
||||||
runCmdCaptWithSpinner(cmd, "evaluating derivations")
|
# runCmdCaptWithSpinner(cmd, "evaluating derivations")
|
||||||
fromJson(output, Table[string, NixDerivation])
|
# fromJson(output, Table[string, NixDerivation])
|
||||||
|
|
||||||
proc nixDerivationShow*(drvs: openArray[string]): Table[string, NixDerivation] =
|
proc nixDerivationShow*(drvs: openArray[string]): Table[string, NixDerivation] =
|
||||||
var cmd = "nix derivation show"
|
var cmd = "nix derivation show"
|
||||||
|
@ -199,35 +191,9 @@ proc nixDerivationShow*(drvs: openArray[string]): Table[string, NixDerivation] =
|
||||||
runCmdCaptWithSpinner(cmd, "evaluating " & drvs.join(" "))
|
runCmdCaptWithSpinner(cmd, "evaluating " & drvs.join(" "))
|
||||||
fromJson(output, Table[string, NixDerivation])
|
fromJson(output, Table[string, NixDerivation])
|
||||||
|
|
||||||
|
proc getSystemPathDrvs*(): seq[string] =
|
||||||
# TODO: replace asserts in this proc, would be easier with results type
|
for drv, _ in nixDerivationShow(nixosAttrs("path")):
|
||||||
proc findSystemPaths(drvs: Table[string, NixDerivation]): seq[string] =
|
result.add drv
|
||||||
let hosts = getHosts()
|
|
||||||
let systemDrvs = collect(
|
|
||||||
for k in drvs.keys():
|
|
||||||
if k.split("-",1)[1].startswith("nixos-system-"): k
|
|
||||||
)
|
|
||||||
|
|
||||||
assert len(hosts) == len(systemDrvs)
|
|
||||||
for name in systemDrvs:
|
|
||||||
for drv in drvs[name].inputDrvs.keys():
|
|
||||||
if drv.endsWith("system-path.drv"):
|
|
||||||
result.add drv
|
|
||||||
|
|
||||||
assert len(hosts) == len(result)
|
|
||||||
|
|
||||||
# NOTE: is find system paths always called after a nixDerivationShow?
|
|
||||||
proc getSystemPaths*(): seq[string] =
|
|
||||||
let systemDrvs = nixDerivationShow(nixosConfigAttrs())
|
|
||||||
result = findSystemPaths(systemDrvs)
|
|
||||||
|
|
||||||
proc filterSeq(
|
|
||||||
drvs: seq[string],
|
|
||||||
filter: proc(s: string): bool,
|
|
||||||
): tuple[yes: seq[string], no: seq[string]] =
|
|
||||||
for drv in drvs:
|
|
||||||
if filter(drv): result.yes.add drv
|
|
||||||
else: result.no.add drv
|
|
||||||
|
|
||||||
func getIgnoredPackages(): seq[string] =
|
func getIgnoredPackages(): seq[string] =
|
||||||
for l in slurp("ignored.txt").strip().splitLines():
|
for l in slurp("ignored.txt").strip().splitLines():
|
||||||
|
@ -243,9 +209,8 @@ func isIgnored(drv: string): bool =
|
||||||
if name.startswith(pkg):
|
if name.startswith(pkg):
|
||||||
return true
|
return true
|
||||||
|
|
||||||
proc getSystemPathDrvs*(): seq[string] =
|
proc getSystemPathInputDrvs*(): seq[string] =
|
||||||
let systemDrvs = nixDerivationShow(nixosConfigAttrs())
|
let systemPathDrvs = getSystemPathDrvs()
|
||||||
let systemPathDrvs = findSystemPaths(systemDrvs)
|
|
||||||
|
|
||||||
result =
|
result =
|
||||||
collect:
|
collect:
|
||||||
|
@ -253,85 +218,57 @@ proc getSystemPathDrvs*(): seq[string] =
|
||||||
for inputDrv, _ in drv.inputDrvs:
|
for inputDrv, _ in drv.inputDrvs:
|
||||||
inputDrv
|
inputDrv
|
||||||
|
|
||||||
proc getOizysDerivations():Table[string, NixDerivation] =
|
proc missingDerivations*():Table[string, NixDerivation] =
|
||||||
let
|
let
|
||||||
toBuildDrvs = toBuildNixosConfiguration()
|
toBuildDrvs = toBuildNixosConfiguration()
|
||||||
systemPathDrvs = getSystemPathDrvs()
|
systemPathInputDrvs = getSystemPathInputDrvs()
|
||||||
toActullyBuildDrvs = systemPathDrvs.filterIt(it in toBuildDrvs and not isIgnored(it))
|
toActullyBuildDrvs = systemPathInputDrvs.filterIt(it in toBuildDrvs and not isIgnored(it))
|
||||||
|
|
||||||
for path , drv in nixDerivationShow(toActullyBuildDrvs):
|
for path , drv in nixDerivationShow(toActullyBuildDrvs):
|
||||||
result[path] = drv
|
result[path] = drv
|
||||||
|
|
||||||
proc showOizysDerivations*() =
|
func fmtDrvsForNix*(drvs: seq[string]): string {.inline.} =
|
||||||
let drvs = getOizysDerivations()
|
drvs.mapIt(it & "^*").join(" ")
|
||||||
for path, drv in drvs:
|
|
||||||
echo path & "^*"
|
|
||||||
|
|
||||||
# TODO: remove this proc
|
|
||||||
proc systemPathDrvsToBuild*(): seq[string] =
|
|
||||||
var inputDrvs, dropped: seq[string]
|
|
||||||
let toBuild = toBuildNixosConfiguration()
|
|
||||||
let drvs = evaluateDerivations(nixosConfigAttrs())
|
|
||||||
|
|
||||||
let systemPaths = findSystemPaths(drvs)
|
|
||||||
for p in systemPaths:
|
|
||||||
inputDrvs &= drvs[p].inputDrvs.keys().toSeq()
|
|
||||||
|
|
||||||
(result, _) = filterSeq(inputDrvs, (s) => s in toBuild)
|
|
||||||
(dropped, result) = filterSeq(result, isIgnored)
|
|
||||||
debug fmt"ignored {dropped.len} derivations"
|
|
||||||
result = result.mapIt(it & "^*")
|
|
||||||
|
|
||||||
|
func fmtDrvsForNix*(drvs: Table[string, NixDerivation]): string {.inline.} =
|
||||||
|
let formatted =
|
||||||
|
collect:
|
||||||
|
for k, _ in drvs:
|
||||||
|
k & "^*"
|
||||||
|
formatted.join(" ")
|
||||||
|
|
||||||
func splitDrv(drv: string): tuple[name, hash:string] =
|
func splitDrv(drv: string): tuple[name, hash:string] =
|
||||||
assert drv.startsWith("/nix/store"), "is this a /nix/store path? $1" % [drv]
|
assert drv.startsWith("/nix/store"), "is this a /nix/store path? $1" % [drv]
|
||||||
let s = drv.split("-", 1)
|
let s = drv.split("-", 1)
|
||||||
(s[1].replace(".drv",""),s[0].split("/")[^1])
|
(s[1].replace(".drv",""),s[0].split("/")[^1])
|
||||||
|
|
||||||
proc writeDervationsToStepSummary(drvs: seq[string]) =
|
|
||||||
let rows = collect(
|
|
||||||
for drv in drvs:
|
|
||||||
let (name,hash) = splitDrv(drv)
|
|
||||||
fmt"| {name} | `{hash}` |"
|
|
||||||
)
|
|
||||||
let summaryFilePath = getEnv("GITHUB_STEP_SUMMARY")
|
|
||||||
if summaryFilePath == "": fatalQuit "no github step summary found"
|
|
||||||
let output = open(summaryFilePath,fmAppend)
|
|
||||||
output.writeLine("| derivation | hash |\n|---|---|")
|
|
||||||
output.writeLine(rows.join("\n"))
|
|
||||||
close output
|
|
||||||
|
|
||||||
proc nixBuild*(minimal: bool, nom: bool, rest: seq[string]) =
|
proc nixBuild*(minimal: bool, nom: bool, rest: seq[string]) =
|
||||||
var cmd = nixCommand("build", nom)
|
var cmd = nixCommand("build", nom)
|
||||||
if minimal:
|
if minimal:
|
||||||
debug "populating args with derivations not built/cached"
|
debug "populating args with derivations not built/cached"
|
||||||
let drvs = systemPathDrvsToBuild()
|
let drvs = missingDerivations()
|
||||||
if drvs.len == 0:
|
if drvs.len == 0:
|
||||||
info "nothing to build"
|
info "nothing to build"
|
||||||
quit "exiting...", QuitSuccess
|
quit "exiting...", QuitSuccess
|
||||||
cmd.addArgs drvs
|
cmd.addArgs drvs.fmtDrvsForNix()
|
||||||
cmd.addArg "--no-link"
|
cmd.addArgs "--no-link"
|
||||||
if isCi():
|
# if isCi():
|
||||||
writeDervationsToStepSummary drvs
|
# writeDervationsToStepSummary drvs
|
||||||
cmd.addArgs rest
|
cmd.addArgs rest
|
||||||
quitWithCmd cmd
|
quitWithCmd cmd
|
||||||
|
|
||||||
|
|
||||||
proc nixBuildHostDry*(minimal: bool, rest: seq[string]) =
|
proc nixBuildHostDry*(minimal: bool, rest: seq[string]) =
|
||||||
var cmd = nixCommand("build")
|
var cmd = nixCommand("build")
|
||||||
if minimal:
|
if minimal:
|
||||||
debug "populating args with derivations not built/cached"
|
debug "populating args with derivations not built/cached"
|
||||||
let drvs = systemPathDrvsToBuild()
|
let drvs = missingDerivations()
|
||||||
if drvs.len == 0:
|
if drvs.len == 0:
|
||||||
info "nothing to build"
|
info "nothing to build"
|
||||||
quit "exiting...", QuitSuccess
|
quit "exiting...", QuitSuccess
|
||||||
cmd.addArgs drvs
|
cmd.addArgs drvs.fmtDrvsForNix()
|
||||||
cmd.addArg "--no-link"
|
cmd.addArg "--no-link"
|
||||||
|
|
||||||
if isCi():
|
|
||||||
writeDervationsToStepSummary drvs
|
|
||||||
else:
|
else:
|
||||||
cmd.addArgs nixosConfigAttrs()
|
cmd.addArgs nixosAttrs()
|
||||||
|
|
||||||
cmd.addArg "--dry-run"
|
cmd.addArg "--dry-run"
|
||||||
cmd.addArgs rest
|
cmd.addArgs rest
|
||||||
let (_, err) =
|
let (_, err) =
|
||||||
|
@ -343,6 +280,7 @@ proc nixBuildHostDry*(minimal: bool, rest: seq[string]) =
|
||||||
let output = parseDryRunOutput err
|
let output = parseDryRunOutput err
|
||||||
display output
|
display output
|
||||||
|
|
||||||
|
# NOTE: two above procs are redundant
|
||||||
|
|
||||||
type
|
type
|
||||||
BuildResult = object
|
BuildResult = object
|
||||||
|
@ -455,7 +393,7 @@ proc nixBuildWithCache*(name: string, rest: seq[string], service: string, jobs:
|
||||||
let cache = toCache(service, name)
|
let cache = toCache(service, name)
|
||||||
debug "determining missing cache hits"
|
debug "determining missing cache hits"
|
||||||
|
|
||||||
let drvs = getOizysDerivations()
|
let drvs = missingDerivations()
|
||||||
if drvs.len == 0:
|
if drvs.len == 0:
|
||||||
info "nothing to build"
|
info "nothing to build"
|
||||||
quit "exiting...", QuitSuccess
|
quit "exiting...", QuitSuccess
|
||||||
|
@ -493,7 +431,7 @@ proc getUpdatedLockFile() =
|
||||||
# probably duplicating logic above ¯\_(ツ)_/¯
|
# probably duplicating logic above ¯\_(ツ)_/¯
|
||||||
proc buildSystem(host: string, rest: seq[string]) =
|
proc buildSystem(host: string, rest: seq[string]) =
|
||||||
var cmd = nixCommand("build")
|
var cmd = nixCommand("build")
|
||||||
cmd.addArg nixosConfigAttr(host)
|
cmd.addArg nixosAttr(host)
|
||||||
cmd.addArgs rest
|
cmd.addArgs rest
|
||||||
let code = runCmd cmd
|
let code = runCmd cmd
|
||||||
if code != 0:
|
if code != 0:
|
||||||
|
|
Loading…
Add table
Reference in a new issue