cli: fallback to bundled Nixpkgs

This commit is contained in:
Michael Hoang
2025-04-25 11:11:51 +10:00
parent 7100277599
commit 2f95d2edf2
2 changed files with 16 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ from tempfile import NamedTemporaryFile
from typing import Any
from clan_cli.cmd import Log, RunOpts, run
from clan_cli.dirs import select_source, user_cache_dir
from clan_cli.dirs import nixpkgs_source, select_source, user_cache_dir
from clan_cli.errors import ClanError
from clan_cli.nix import (
nix_build,
@@ -701,6 +701,18 @@ class Flake:
config = nix_config()
# these hashes should be filled in by `nix build`
# if we run this Python code directly then we use a fallback
# method to getting the NAR hash
fallback_nixpkgs_hash = "@fallback_nixpkgs_hash@"
if not fallback_nixpkgs_hash.startswith("sha256-"):
fallback_nixpkgs = Flake(str(nixpkgs_source()))
fallback_nixpkgs.invalidate_cache()
assert fallback_nixpkgs.hash is not None, (
"this should be impossible as invalidate_cache() should always set `hash`"
)
fallback_nixpkgs_hash = fallback_nixpkgs.hash
select_hash = "@select_hash@"
if not select_hash.startswith("sha256-"):
select_flake = Flake(str(select_source()))
@@ -714,8 +726,9 @@ class Flake:
let
flake = builtins.getFlake "path:{self.store_path}?narHash={self.hash}";
selectLib = (builtins.getFlake "{select_source()}?narHash={select_hash}").lib;
nixpkgs = flake.inputs.nixpkgs or (builtins.getFlake "{nixpkgs_source()}?narHash={fallback_nixpkgs_hash}");
in
flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" (
nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" (
builtins.toJSON [ {" ".join([f"(selectLib.applySelectors (builtins.fromJSON ''{attr}'') flake)" for attr in str_selectors])} ]
)
"""

View File

@@ -66,6 +66,7 @@ let
rm -f $out/clan_cli/nixpkgs
substituteInPlace $out/clan_cli/flake.py \
--replace-fail '@fallback_nixpkgs_hash@' "$(jq -r '.nodes.nixpkgs.locked.narHash' ${nixpkgs'}/flake.lock)" \
--replace-fail '@select_hash@' "$(jq -r '.nodes."nix-select".locked.narHash' ${../../flake.lock})"
ln -sf ${nixpkgs'} $out/clan_cli/nixpkgs
ln -sf ${nix-select} $out/clan_cli/select