diff --git a/_sources/generated.json b/_sources/generated.json index bc5918f..17cfd37 100644 --- a/_sources/generated.json +++ b/_sources/generated.json @@ -1,9 +1,9 @@ { - "freetube": { + "mender-cli": { "cargoLock": null, - "date": "2026-03-18", + "date": null, "extract": null, - "name": "freetube", + "name": "mender-cli", "passthru": null, "pinned": false, "src": { @@ -11,13 +11,14 @@ "fetchSubmodules": false, "leaveDotGit": false, "name": null, - "rev": "d5cf1d713ba9348b13d94c122ba0fb72694e51d3", - "sha256": "sha256-g9bMYIxHXcL8+apBD682sZHeifLfyVhI5oTNoPPNs5o=", + "owner": "mendersoftware", + "repo": "mender-cli", + "rev": "2.0.0", + "sha256": "sha256-jFWC1g/ebgMIqgBFgotBDwPcGNmAK3VsvfjiuRCN1ws=", "sparseCheckout": [], - "type": "git", - "url": "https://github.com/ddogfoodd/nixpkgs.git" + "type": "github" }, - "version": "d5cf1d713ba9348b13d94c122ba0fb72694e51d3" + "version": "2.0.0" }, "valheim.aaacrafting": { "cargoLock": null, diff --git a/_sources/generated.nix b/_sources/generated.nix index e3833c5..68f3e7b 100644 --- a/_sources/generated.nix +++ b/_sources/generated.nix @@ -6,19 +6,17 @@ dockerTools, }: { - freetube = { - pname = "freetube"; - version = "d5cf1d713ba9348b13d94c122ba0fb72694e51d3"; - src = fetchgit { - url = "https://github.com/ddogfoodd/nixpkgs.git"; - rev = "d5cf1d713ba9348b13d94c122ba0fb72694e51d3"; + mender-cli = { + pname = "mender-cli"; + version = "2.0.0"; + vendorHash = "sha256-vlcq9HGezJsorwsaVwvIKxi1pSoo4wTPmc6qCCVDdfI="; + src = fetchFromGitHub { + owner = "mendersoftware"; + repo = "mender-cli"; + rev = "2.0.0"; fetchSubmodules = false; - deepClone = false; - leaveDotGit = false; - sparseCheckout = [ ]; - sha256 = "sha256-g9bMYIxHXcL8+apBD682sZHeifLfyVhI5oTNoPPNs5o="; + sha256 = "sha256-jFWC1g/ebgMIqgBFgotBDwPcGNmAK3VsvfjiuRCN1ws="; }; - date = "2026-03-18"; }; "valheim.aaacrafting" = { pname = "valheim.aaacrafting"; diff --git a/hosts/murk/home.nix b/hosts/murk/home.nix index fc55663..6276198 100644 --- a/hosts/murk/home.nix +++ b/hosts/murk/home.nix @@ -46,7 +46,6 @@ in { go rainfrog tealdeer - (callPackage ./packages/mender-cli.nix {}) ] # Non-free `modules/home/terminal/hr/mender-cli.nix` + +To add more Go packages, edit `pkgs/nvft/default.nix` and add entries to the `GO_PACKAGES` array. + +## How it Works + +For each Go package: +1. Compares old vs new source hash to detect updates +2. If source unchanged, keeps existing vendorHash (fast) +3. If source changed, creates a temporary build with `lib.fakeHash` +4. Attempts to build, which fails with the correct hash +5. Extracts the correct hash from the error message +6. Updates `vendorHash` directly in `_sources/generated.nix` alongside the source information + +## Package Usage + +Go packages read vendorHash from the sources: + +```nix +let + src = sources.mender-cli; +in +buildGoModule { + inherit (src) pname version src vendorHash; + # ... rest of package +} +``` + +The vendorHash is stored in `_sources/generated.nix` alongside the version and source hash, keeping all auto-generated values in one place. diff --git a/pkgs/nvft/default.nix b/pkgs/nvft/default.nix new file mode 100644 index 0000000..0767c39 --- /dev/null +++ b/pkgs/nvft/default.nix @@ -0,0 +1,250 @@ +{ + lib, + writeShellApplication, + nvfetcher, + nix, + gnused, + coreutils, +}: +writeShellApplication { + name = "nvft"; + + runtimeInputs = [ + nvfetcher + nix + gnused + coreutils + ]; + + text = '' + set -euo pipefail + + # Determine the config directory + if [[ -n "''${NVFETCHER_CONFIG_DIR:-}" ]]; then + CONFIG_DIR="$NVFETCHER_CONFIG_DIR" + elif [[ -f "nvfetcher.toml" ]]; then + CONFIG_DIR="$(pwd)" + else + CONFIG_DIR="''${HOME}/.config/home" + fi + + if [[ ! -f "$CONFIG_DIR/nvfetcher.toml" ]]; then + echo "❌ Error: nvfetcher.toml not found in $CONFIG_DIR" + exit 1 + fi + + echo "🚀 Updating sources in $CONFIG_DIR" + echo "" + + SOURCES_FILE="$CONFIG_DIR/_sources/generated.nix" + + # Read Go packages from nvfetcher.toml that need vendorHash updates + declare -A GO_PACKAGES + # For now, hardcode the known Go packages + # TODO: Could parse nvfetcher.toml to auto-detect these + GO_PACKAGES["mender-cli"]="modules/home/terminal/hr/mender-cli.nix" + + # Step 0: Save existing hashes before nvfetcher wipes them + echo "💾 Saving existing hashes..." + declare -A SAVED_VENDOR_HASHES + declare -A SAVED_SOURCE_HASHES + + if [[ -f "$SOURCES_FILE" ]]; then + for source_name in "''${!GO_PACKAGES[@]}"; do + # Save vendorHash + saved_vendor=$(grep -A20 "^ $source_name = " "$SOURCES_FILE" | grep -oP 'vendorHash = "\K[^"]+' || echo "") + if [[ -n "$saved_vendor" ]]; then + SAVED_VENDOR_HASHES["$source_name"]="$saved_vendor" + echo " Saved $source_name vendorHash: $saved_vendor" + fi + + # Save source sha256 to detect if source changed + saved_source=$(grep -A20 "^ $source_name = " "$SOURCES_FILE" | grep -oP 'sha256 = "\K[^"]+' || echo "") + if [[ -n "$saved_source" ]]; then + SAVED_SOURCE_HASHES["$source_name"]="$saved_source" + echo " Saved $source_name sourceHash: $saved_source" + fi + done + fi + echo "" + + # Step 1: Run nvfetcher + echo "đŸ“Ļ Step 1: Running nvfetcher..." + cd "$CONFIG_DIR" + if nvfetcher; then + echo "" + echo "✅ nvfetcher completed" + echo "" + else + echo "" + echo "âš ī¸ nvfetcher had some errors, but continuing with vendor hash updates..." + echo "" + fi + + # Step 2: Restore saved vendorHash values + echo "đŸ“Ļ Step 2: Restoring saved vendorHash values..." + for source_name in "''${!SAVED_VENDOR_HASHES[@]}"; do + saved_hash="''${SAVED_VENDOR_HASHES[$source_name]}" + echo " Restoring $source_name: $saved_hash" + + # Add vendorHash back after version line using awk + awk -v source="$source_name" -v hash="$saved_hash" ' + /^ / && $0 ~ source" = " {in_block=1} + in_block && /version = / {print; print " vendorHash = \"" hash "\";"; next} + in_block && /^ };/ {in_block=0} + {print} + ' "$SOURCES_FILE" > "$SOURCES_FILE.tmp" && mv "$SOURCES_FILE.tmp" "$SOURCES_FILE" + done + echo "" + + # Step 3: Update Go vendor hashes + echo "đŸ“Ļ Step 3: Checking for Go packages that need vendor hash updates..." + + if [[ ! -f "$SOURCES_FILE" ]]; then + echo "❌ Error: Generated sources file not found at $SOURCES_FILE" + exit 1 + fi + + if [[ ''${#GO_PACKAGES[@]} -eq 0 ]]; then + echo "â„šī¸ No Go packages configured for vendor hash updates" + echo "" + echo "🎉 All updates complete!" + exit 0 + fi + + for source_name in "''${!GO_PACKAGES[@]}"; do + package_path="''${GO_PACKAGES[$source_name]}" + full_path="$CONFIG_DIR/$package_path" + + if [[ ! -f "$full_path" ]]; then + echo "âš ī¸ Package file not found: $full_path" + continue + fi + + echo "" + echo "đŸ“Ļ Processing $source_name..." + + # Check if vendorHash already exists in generated.nix + current_vendor=$(grep -A20 "^ $source_name = " "$SOURCES_FILE" | grep -oP 'vendorHash = "\K[^"]+' || echo "") + + if [[ -n "$current_vendor" ]]; then + echo " Current vendorHash: $current_vendor" + else + echo " No vendorHash found" + fi + + # Check if source hash changed (indicates version/source update) + new_source_hash=$(grep -A20 "^ $source_name = " "$SOURCES_FILE" | grep -oP 'sha256 = "\K[^"]+' || echo "") + old_source_hash="''${SAVED_SOURCE_HASHES[$source_name]:-}" + + if [[ -n "$old_source_hash" ]] && [[ "$old_source_hash" == "$new_source_hash" ]]; then + echo " Source unchanged (hash: ''${new_source_hash:0:16}...)" + echo " ✨ Skipping vendorHash recalculation" + continue + elif [[ -n "$old_source_hash" ]]; then + echo " Source changed!" + echo " Old: ''${old_source_hash:0:16}..." + echo " New: ''${new_source_hash:0:16}..." + echo " → Need to recalculate vendorHash" + else + echo " New package, calculating vendorHash..." + fi + + # Get version from generated sources + new_version=$(nix-instantiate --eval --strict --expr " + let pkgs = import {}; + sources = pkgs.callPackage $SOURCES_FILE {}; + in sources.''${source_name}.version + " 2>/dev/null | tr -d '"' || echo "") + + if [[ -z "$new_version" ]]; then + echo "âš ī¸ Could not determine version from generated sources" + continue + fi + + echo " Version: $new_version" + + # Create temporary build directory + temp_build=$(mktemp -d) + trap 'rm -rf "$temp_build"' EXIT + + # Copy necessary files + cp -r "$CONFIG_DIR/_sources" "$temp_build/" + cp "$full_path" "$temp_build/package.nix" + + # Create build expression + cat > "$temp_build/default.nix" << 'NIXEOF' + { pkgs ? import {} }: + let + sources = pkgs.callPackage ./_sources/generated.nix {}; + in + pkgs.callPackage ./package.nix { inherit sources; } + NIXEOF + + # Temporarily replace hash with fakeHash + # Replace both inherited vendorHash and explicit vendorHash assignments + sed -i 's|inherit (src) pname version src vendorHash|inherit (src) pname version src;\n vendorHash = lib.fakeHash|g' "$temp_build/package.nix" + sed -i 's|vendorHash = "[^"]*";|vendorHash = lib.fakeHash;|g' "$temp_build/package.nix" + + echo " Calculating correct vendor hash..." + + # Build and extract the correct hash from error message + vendor_hash=$(nix-build "$temp_build/default.nix" 2>&1 | \ + grep -oP 'got:\s+\K(sha256-[A-Za-z0-9+/=]+)' | \ + head -1 || echo "") + + if [[ -z "$vendor_hash" ]]; then + echo "âš ī¸ Could not calculate vendor hash" + echo " The package may have built successfully (current hash is correct)" + echo " Or there may be a build error unrelated to vendorHash" + continue + fi + + echo " New hash: $vendor_hash" + + # Update the generated.nix file to include vendorHash + # Find the entry and add/update vendorHash after version + if grep -q "^ $source_name = " "$SOURCES_FILE"; then + # Check if vendorHash already exists + if grep -A20 "^ $source_name = " "$SOURCES_FILE" | grep -q "vendorHash"; then + # Update existing vendorHash using awk for better multiline handling + awk -v source="$source_name" -v hash="$vendor_hash" ' + /^ / && $0 ~ source" = " {in_block=1} + in_block && /vendorHash = / {sub(/vendorHash = "[^"]*"/, "vendorHash = \"" hash "\"")} + in_block && /^ };/ {in_block=0} + {print} + ' "$SOURCES_FILE" > "$SOURCES_FILE.tmp" && mv "$SOURCES_FILE.tmp" "$SOURCES_FILE" + echo "✅ Updated vendorHash in generated.nix" + else + # Add vendorHash after version line using awk + awk -v source="$source_name" -v hash="$vendor_hash" ' + /^ / && $0 ~ source" = " {in_block=1} + in_block && /version = / {print; print " vendorHash = \"" hash "\";"; next} + in_block && /^ };/ {in_block=0} + {print} + ' "$SOURCES_FILE" > "$SOURCES_FILE.tmp" && mv "$SOURCES_FILE.tmp" "$SOURCES_FILE" + echo "✅ Added vendorHash to generated.nix" + fi + else + echo "âš ī¸ Could not find $source_name entry in generated.nix" + fi + done + + echo "" + echo "🎉 All updates complete!" + echo "" + echo "Summary:" + echo " - Source versions and hashes updated by nvfetcher" + echo " - Go vendor hashes recalculated and updated" + echo "" + echo "Next steps:" + echo " - Review changes: git diff" + echo " - Test build: home-manager switch or similar" + echo " - Commit if everything works" + ''; + + meta = { + description = "Update nvfetcher sources and Go vendor hashes"; + mainProgram = "nvft"; + }; +} diff --git a/shell.nix b/shell.nix index ffc8955..1da75bc 100644 --- a/shell.nix +++ b/shell.nix @@ -1,4 +1,8 @@ -{pkgs ? import {}, ...}: { +{pkgs ? import {}, ...}: +let + nvft = pkgs.callPackage ./pkgs/nvft {}; +in +{ default = pkgs.mkShell { NIX_CONFIG = "extra-experimental-features = nix-command flakes ca-derivations"; nativeBuildInputs = with pkgs; [ @@ -8,6 +12,7 @@ sops age nvfetcher + nvft ]; }; }