Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-update: add, use for updates, simply main.nu #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
flake-compat = {
url = "github:nix-community/flake-compat";
};
nix-update = {
url = "github:Mic92/nix-update";
};
};

nixConfig = {
Expand Down Expand Up @@ -367,6 +370,7 @@
ripgrep
sd
inputs.nix-eval-jobs.outputs.packages.${system}.default
inputs.nix-update.outputs.packages.${system}.default
];
};

Expand Down
140 changes: 23 additions & 117 deletions main.nu
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@ $env.CACHIX_SIGNING_KEY = (
else "null"
)

def header [ color: string text: string spacer="▒": string ] {
let text = $"($text) "
let header = $"("" | fill -c $spacer -w 2) ($text | fill -c $spacer -w 100)"
print -e $"(ansi $color)($header)(ansi reset)"
}

def getBadHash [ attrName: string ] {
let val = ((do -i { ^nix build --no-link $attrName }| complete)
| get stderr
| split row "\n"
| where ($it | str contains "got:")
| str replace --regex '\s+got:(.*)(sha256-.*)' '$2'
| get 0
)
$val
}

def replaceHash [ packageName: string, position: string, hashName: string, oldHash: string ] {
let fakeSha256 = "0000000000000000000000000000000000000000000000000000";

Expand All @@ -46,101 +29,22 @@ def updatePkg [packageName: string] {

let skip = (("skip" in ($verinfo | transpose | get column0)) and $verinfo.skip)
if $skip {
print -e $"(ansi light_yellow) update ($packageName) - (ansi light_cyan_underline)skipped(ansi reset)"
} else {
# Try update rev
let newrev = (
if ("repo_git" in ($verinfo | transpose | get column0)) {
(do -c {
^git ls-remote $verinfo.repo_git $"refs/heads/($verinfo.branch)"
} | complete | get stdout | str trim | str replace --regex '(\s+)(.*)$' "")
} else if ( "repo_hg" in ($verinfo | transpose | get column0) ) {
(do -c {
^hg identify $verinfo.repo_hg -r $verinfo.branch
} | complete | get stdout | str trim)
} else {
error make { msg: "unknown repo type" }
}
)

let shouldUpdate = (if ($forceCheck) {
print -e $"(ansi light_yellow) update ($packageName) - (ansi light_yellow_underline)forced(ansi reset)"
true
} else if ($newrev != $verinfo.rev) {
print -e $"(ansi light_yellow) update ($packageName) - (ansi light_yellow_underline)update to ($newrev)(ansi reset)"
true
} else {
print -e $"(ansi dark_gray) update ($packageName) - noop(ansi reset)"
false
})

if ($shouldUpdate) {
do -c { ^sd -s $"($verinfo.rev)" $"($newrev)" $"($position)" }
print -e {packageName: $packageName, oldrev: $verinfo.rev, newrev: $newrev}

replaceHash $packageName $position "sha256" $verinfo.sha256
if "vendorSha256" in ($verinfo | transpose | get column0) {
replaceHash $packageName $position "vendorSha256" $verinfo.vendorSha256
}

do -c {
^git commit $position -m $"auto-update: ($packageName): ($verinfo.rev) => ($newrev)"
} | complete
}
return
}

null
} # end !skip
nix-update --flake $"packages.x86_64-linux.($packageName)" --version branch --commit --override-filename ./pkgs/($packageName)/metadata.nix
}

def updatePkgs [] {
header "light_yellow_reverse" "update packages"
let pkgs = (^nix eval --json $".#packages.($system)" --apply 'x: builtins.attrNames x' | str trim | from json)
let pkgs = ($pkgs | where ($it != "default"))
$pkgs | each { |packageName|
updatePkg $packageName
} # end each-pkg loop
}

def buildDrv [ drvRef: string ] {
header "white_reverse" $"build ($drvRef)" "░"
header "blue_reverse" $"eval ($drvRef)"
let evalJobs = (
^nix-eval-jobs
--flake $".#($drvRef)"
| from json --objects
)

header "green_reverse" $"build ($drvRef)"
print -e ($evalJobs
| select name)

$evalJobs
| each { |drv| do -c { ^nix build $'($drv.drvPath)^*' } }

header "purple_reverse" $"cache: calculate paths: ($drvRef)"
let pushPaths = ($evalJobs | each { |drv|
$drv.outputs | each { |outPath|
if ($outPath.out | path exists) {
$outPath.out
}
}
})
print -e $pushPaths

if ($env.CACHIX_SIGNING_KEY != "null") {
let cachePathsStr = ($pushPaths | each {|it| $"($it)(char nl)"} | str join)

let cacheResults = (echo $cachePathsStr | ^cachix push $env.CACHIX_CACHE | complete)
header "purple_reverse" $"cache/push ($drvRef)"
print -e $cacheResults
} else {
print -e "'$CACHIX_SIGNING_KEY_NIXPKGS_WAYLAND' not set, not pushing to cachix."
}
}

def "main rereadme" [] {
let color = "yellow"
header $"($color)_reverse" $"readme"
let packageNames = (nix eval --json $".#packages.($system)" --apply 'x: builtins.attrNames x' | str trim | from json)
let pkgList = ($packageNames | where ($it != "default"))
let delimStart = "<!--pkgs-start-->"
Expand Down Expand Up @@ -171,40 +75,42 @@ def "main rereadme" [] {
do -i { ^git commit -m "auto-update: updated readme" "./README.md" }
}

def "main build" [] {
buildDrv $"packages.($system)"
print -e ""
buildDrv $"devShells.($system).default.inputDerivation"
}

def flakeAdvance [] {
header "purple_reverse" "advance flake inputs"
^nix flake lock --recreate-lock-file --commit-lock-file
}

def gitPush [] {
header "purple_reverse" "git push origin HEAD"
print -e ":: git push origin HEAD"
^git push origin HEAD
}

def "main advance" [] {
flakeAdvance
main build
gitPush
}
def "main build" [] {
print -e ":: nix build bundle (cachix)"
^nix build --keep-going --print-out-paths '.#bundle.x86_64-linux' | cachix push $env.CACHIX_CACHE

def "main update1" [packageName: string] {
updatePkg $packageName
print -e ":: nix build devshell-inputDrv (cachix)"
^nix build --keep-going --print-out-paths $"devShells.($system).default.inputDerivation" | cachix push $env.CACHIX_CACHE
}

def "main update" [] {
def "main advance" [] {
flakeAdvance
updatePkgs
main build
main rereadme
gitPush
}

def "main update" [packageName?: string] {
print -e ":: update"
if $packageName == null {
flakeAdvance
updatePkgs
main build
main rereadme
gitPush
} else {
updatePkg $packageName
}
}

def main [] {
print -e "commands: [advance, update, build]"
}