feat(nix): Package helper scripts into its own derivation

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2025-01-04 11:15:15 +01:00
parent 996b1b8a7f
commit 3631ca0214
6 changed files with 74 additions and 17 deletions
+1
View File
@@ -54,6 +54,7 @@ jobs:
nix-update gocov-merger
nix-update cunicu
nix-update cunicu-scripts
git diff --quiet || echo "changed=true" >> "$GITHUB_OUTPUT"
+1 -1
View File
@@ -80,7 +80,7 @@ docs-website: docs
redirects:
cd scripts && \
go run ./generate_vanity_redirects.go -static-dir ../website/static
go run ./vanity_redirects -static-dir ../website/static
completions: completions/cunicu.bash completions/cunicu.zsh completions/cunicu.fish
+2
View File
@@ -26,6 +26,7 @@
overlays = {
default = final: prev: {
cunicu = final.callPackage ./nix/cunicu.nix { };
cunicu-scripts = final.callPackage ./nix/scripts.nix { };
gocov-merger = final.callPackage ./nix/gocov-merger.nix { };
};
};
@@ -66,6 +67,7 @@
inherit (pkgs) cunicu gocov-merger;
default = pkgs.cunicu;
scripts = pkgs.cunicu-scripts;
};
};
};
+17
View File
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2025 Steffen Vogel <post@steffenvogel.de>
# SPDX-License-Identifier: Apache-2.0
{ buildGoModule, cunicu }:
buildGoModule {
pname = "scripts";
inherit (cunicu) version;
src = ../scripts;
subPackages = [ "vanity_redirects" ];
vendorHash = "sha256-AGepsQRIFVGTWRUQPnSuLEJb/Oxp2G+V0QxBOgb2L1U=";
meta = {
mainProgram = "vanity_redirects";
};
}
+4
View File
@@ -19,6 +19,8 @@
reuse,
svu,
cunicu-scripts,
...
}:
mkShell {
@@ -35,6 +37,8 @@ mkShell {
nix-update
reuse
svu
cunicu-scripts
];
inputsFrom = [
@@ -5,6 +5,7 @@ package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"html/template"
@@ -81,7 +82,7 @@ func getModsFromGitHub(ctx context.Context, owner string) (map[string]string, er
client = client.WithAuthToken(token)
}
repos, _, err := client.Repositories.ListByUser(context.Background(), owner, nil) //nolint:contextcheck
repos, _, err := client.Repositories.ListByUser(ctx, owner, nil)
if err != nil {
return nil, fmt.Errorf("failed to list repos: %w", err)
}
@@ -127,33 +128,65 @@ func getModBase(pkg string) string {
func main() {
prefix := flag.String("prefix", "cunicu.li", "Prefix")
owner := flag.String("owner", "cunicu", "GitHub user/org")
staticDir := flag.String("static-dir", "./website/static", "Directory in which the generated files should be placed")
modsFile := flag.String("modules-file", "", "JSON file containing a map of modules")
modsFetch := flag.Bool("modules-fetch", false, "Only fetch modules from GitHub")
staticDir := flag.String("static-dir", "", "Directory in which the generated files should be placed")
flag.Parse()
ctx := context.Background()
mods, err := getModsFromGitHub(ctx, *owner)
if err != nil {
log.Fatalf("Failed to list packages: %v", err)
var mods map[string]string
if *modsFile == "" || *modsFetch {
var err error
if mods, err = getModsFromGitHub(ctx, *owner); err != nil {
log.Fatalf("Failed to list packages: %v", err)
}
}
for mod, repo := range mods {
modBase := getModBase(mod)
if !strings.HasPrefix(mod, *prefix) {
continue
if *modsFetch {
if *modsFile == "" {
log.Fatal("-modules-fetch requires -modules-file")
}
if err := generate(mod, repo, *staticDir, *prefix); err != nil {
log.Fatalf("Failed to generate HTML file: %v", err)
modsJSON, err := json.MarshalIndent(mods, "", " ")
if err != nil {
log.Fatalf("Failed to encode modules: %s", err)
}
if mod == modBase {
continue
if err := os.WriteFile(*modsFile, modsJSON, 0o755); err != nil { //nolint:gosec
log.Fatalf("Failed to write modules to file: %s", err)
}
} else if *modsFile != "" {
modsJSON, err := os.ReadFile(*modsFile)
if err != nil {
log.Fatalf("Failed to read modules from file: %s", err)
}
if err := generate(modBase, repo, *staticDir, *prefix); err != nil {
log.Fatalf("Failed to generate HTML file: %v", err)
if err := json.Unmarshal(modsJSON, &mods); err != nil {
log.Fatalf("Failed to decode modules: %s", err)
}
}
if *staticDir != "" {
for mod, repo := range mods {
modBase := getModBase(mod)
if !strings.HasPrefix(mod, *prefix) {
continue
}
if err := generate(mod, repo, *staticDir, *prefix); err != nil {
log.Fatalf("Failed to generate HTML file: %v", err)
}
if mod == modBase {
continue
}
if err := generate(modBase, repo, *staticDir, *prefix); err != nil {
log.Fatalf("Failed to generate HTML file: %v", err)
}
}
}
}