From c347badd7fed1d888b4cec685017a5d5e4954245 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Fri, 25 Apr 2025 11:11:51 +1000 Subject: [PATCH] cli: fallback to bundled Nixpkgs --- pkgs/clan-cli/clan_cli/flake.py | 17 +++++++++++++++-- pkgs/clan-cli/default.nix | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/flake.py b/pkgs/clan-cli/clan_cli/flake.py index b25ef5170..58d16ec99 100644 --- a/pkgs/clan-cli/clan_cli/flake.py +++ b/pkgs/clan-cli/clan_cli/flake.py @@ -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])} ] ) """ diff --git a/pkgs/clan-cli/default.nix b/pkgs/clan-cli/default.nix index 409a94cd7..8e01461e4 100644 --- a/pkgs/clan-cli/default.nix +++ b/pkgs/clan-cli/default.nix @@ -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