cli: don't use select from clanLib

This commit is contained in:
Michael Hoang
2025-04-25 17:44:08 +10:00
parent 52a28488c7
commit 6c8ef6e9be
6 changed files with 55 additions and 22 deletions

View File

@@ -32,7 +32,7 @@ lib.fix (clanLib: {
jsonschema = import ./jsonschema { inherit lib; }; jsonschema = import ./jsonschema { inherit lib; };
facts = import ./facts.nix { inherit lib; }; facts = import ./facts.nix { inherit lib; };
# Passthrough from self.inputs # deprecated
# TODO: Can we make these agnostic from the name of the input? # remove when https://git.clan.lol/clan/clan-core/pulls/3212 is implemented
inherit (self.inputs.nix-select.lib) parseSelector applySelectors select; inherit (self.inputs.nix-select.lib) select;
}) })

View File

@@ -140,3 +140,7 @@ def nixpkgs_flake() -> Path:
def nixpkgs_source() -> Path: def nixpkgs_source() -> Path:
return (module_root() / "nixpkgs" / "path").resolve() return (module_root() / "nixpkgs" / "path").resolve()
def select_source() -> Path:
return (module_root() / "select").resolve()

View File

@@ -8,7 +8,7 @@ from tempfile import NamedTemporaryFile
from typing import Any from typing import Any
from clan_cli.cmd import Log, RunOpts, run from clan_cli.cmd import Log, RunOpts, run
from clan_cli.dirs import user_cache_dir from clan_cli.dirs import select_source, user_cache_dir
from clan_cli.errors import ClanError from clan_cli.errors import ClanError
from clan_cli.nix import ( from clan_cli.nix import (
nix_build, nix_build,
@@ -700,12 +700,23 @@ class Flake:
str_selectors.append(selectors_as_json(parse_selector(selector))) str_selectors.append(selectors_as_json(parse_selector(selector)))
config = nix_config() config = nix_config()
select_hash = "@select_hash@"
if not select_hash.startswith("sha256-"):
select_flake = Flake(str(select_source()))
select_flake.invalidate_cache()
assert select_flake.hash is not None, (
"this should be impossible as invalidate_cache() should always set `hash`"
)
select_hash = select_flake.hash
nix_code = f""" nix_code = f"""
let let
flake = builtins.getFlake("path:{self.store_path}?narHash={self.hash}"); flake = builtins.getFlake "path:{self.store_path}?narHash={self.hash}";
selectLib = (builtins.getFlake "{select_source()}?narHash={select_hash}").lib;
in in
flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" ( flake.inputs.nixpkgs.legacyPackages.{config["system"]}.writeText "clan-flake-select" (
builtins.toJSON [ {" ".join([f"(flake.clanInternals.clanLib.applySelectors (builtins.fromJSON ''{attr}'') flake)" for attr in str_selectors])} ] builtins.toJSON [ {" ".join([f"(selectLib.applySelectors (builtins.fromJSON ''{attr}'') flake)" for attr in str_selectors])} ]
) )
""" """
if tmp_store := nix_test_store(): if tmp_store := nix_test_store():

View File

@@ -2,15 +2,16 @@
# callPackage args # callPackage args
gnupg, gnupg,
installShellFiles, installShellFiles,
jq,
lib, lib,
nix, nix,
pkgs, pkgs,
runCommand, runCommand,
stdenv, stdenv,
nixVersions,
# custom args # custom args
clan-core-path, clan-core-path,
nixpkgs, nixpkgs,
nix-select,
includedRuntimeDeps, includedRuntimeDeps,
pythonRuntime, pythonRuntime,
templateDerivation, templateDerivation,
@@ -52,13 +53,22 @@ let
(pythonRuntime.withPackages pyTestDeps) (pythonRuntime.withPackages pyTestDeps)
]; ];
source = runCommand "clan-cli-source" { } '' source =
runCommand "clan-cli-source"
{
nativeBuildInputs = [ jq ];
}
''
cp -r ${./.} $out cp -r ${./.} $out
chmod -R +w $out chmod -R +w $out
# In cases where the devshell created this file, this will already exist # In cases where the devshell created this file, this will already exist
rm -f $out/clan_cli/nixpkgs rm -f $out/clan_cli/nixpkgs
substituteInPlace $out/clan_cli/flake.py \
--replace-fail '@select_hash@' "$(jq -r '.nodes."nix-select".locked.narHash' ${../../flake.lock})"
ln -sf ${nixpkgs'} $out/clan_cli/nixpkgs ln -sf ${nixpkgs'} $out/clan_cli/nixpkgs
ln -sf ${nix-select} $out/clan_cli/select
cp -r ${../../templates} $out/clan_cli/templates cp -r ${../../templates} $out/clan_cli/templates
''; '';
@@ -67,7 +77,7 @@ let
runCommand "nixpkgs" runCommand "nixpkgs"
{ {
# Not all versions have `nix flake update --flake` option # Not all versions have `nix flake update --flake` option
nativeBuildInputs = [ nixVersions.stable ]; nativeBuildInputs = [ nix ];
} }
'' ''
mkdir $out mkdir $out
@@ -121,14 +131,12 @@ pythonRuntime.pkgs.buildPythonApplication {
propagatedBuildInputs = [ pythonRuntimeWithDeps ] ++ bundledRuntimeDependencies; propagatedBuildInputs = [ pythonRuntimeWithDeps ] ++ bundledRuntimeDependencies;
# Define and expose the tests and checks to run in CI
passthru.tests = passthru.tests =
{ {
clan-deps = pkgs.runCommand "clan-deps" { } '' clan-deps = pkgs.runCommand "clan-deps" { } ''
# ${builtins.toString (builtins.attrValues testRuntimeDependenciesMap)} # ${builtins.toString (builtins.attrValues testRuntimeDependenciesMap)}
touch $out touch $out
''; '';
# disabled on macOS until we fix all remaining issues
clan-pytest-without-core = clan-pytest-without-core =
runCommand "clan-pytest-without-core" runCommand "clan-pytest-without-core"
{ {
@@ -140,7 +148,7 @@ pythonRuntime.pkgs.buildPythonApplication {
}; };
} }
'' ''
set -u -o pipefail set -euo pipefail
cp -r ${source} ./src cp -r ${source} ./src
chmod +w -R ./src chmod +w -R ./src
cd ./src cd ./src
@@ -160,6 +168,7 @@ pythonRuntime.pkgs.buildPythonApplication {
''; '';
} }
// lib.optionalAttrs (!stdenv.isDarwin) { // lib.optionalAttrs (!stdenv.isDarwin) {
# disabled on macOS until we fix all remaining issues
clan-pytest-with-core = clan-pytest-with-core =
runCommand "clan-pytest-with-core" runCommand "clan-pytest-with-core"
{ {
@@ -182,7 +191,7 @@ pythonRuntime.pkgs.buildPythonApplication {
}; };
} }
'' ''
set -u -o pipefail set -euo pipefail
cp -r ${source} ./src cp -r ${source} ./src
chmod +w -R ./src chmod +w -R ./src
cd ./src cd ./src
@@ -215,8 +224,14 @@ pythonRuntime.pkgs.buildPythonApplication {
passthru.testRuntimeDependencies = testRuntimeDependencies; passthru.testRuntimeDependencies = testRuntimeDependencies;
passthru.testRuntimeDependenciesMap = testRuntimeDependenciesMap; passthru.testRuntimeDependenciesMap = testRuntimeDependenciesMap;
# Nixpkgs doesn't get copied from `src` as it's not in `package-data` in `pyproject.toml`
# as it significantly slows down the build so we copy it again here
# We don't copy `select` using `package-data` as Python globs don't include hidden directories
# leading to a different NAR hash and copying it here would also lead to `patchShebangs`
# changing the contents
postInstall = '' postInstall = ''
cp -r ${nixpkgs'} $out/${pythonRuntime.sitePackages}/clan_cli/nixpkgs cp -r ${nixpkgs'} $out/${pythonRuntime.sitePackages}/clan_cli/nixpkgs
ln -sf ${nix-select} $out/${pythonRuntime.sitePackages}/clan_cli/select
installShellCompletion --bash --name clan \ installShellCompletion --bash --name clan \
<(${pythonRuntimeWithDeps.pkgs.argcomplete}/bin/register-python-argcomplete --shell bash clan) <(${pythonRuntimeWithDeps.pkgs.argcomplete}/bin/register-python-argcomplete --shell bash clan)
installShellCompletion --fish --name clan.fish \ installShellCompletion --fish --name clan.fish \

View File

@@ -129,10 +129,11 @@
devShells.clan-cli = pkgs.callPackage ./shell.nix { devShells.clan-cli = pkgs.callPackage ./shell.nix {
inherit (self'.packages) clan-cli; inherit (self'.packages) clan-cli;
inherit self'; inherit self';
inherit (inputs) nix-select;
}; };
packages = { packages = {
clan-cli = pkgs.callPackage ./default.nix { clan-cli = pkgs.callPackage ./default.nix {
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs nix-select;
templateDerivation = templateDerivation; templateDerivation = templateDerivation;
pythonRuntime = pkgs.python3; pythonRuntime = pkgs.python3;
clan-core-path = clanCoreWithVendoredDeps; clan-core-path = clanCoreWithVendoredDeps;
@@ -142,7 +143,7 @@
]; ];
}; };
clan-cli-full = pkgs.callPackage ./default.nix { clan-cli-full = pkgs.callPackage ./default.nix {
inherit (inputs) nixpkgs; inherit (inputs) nixpkgs nix-select;
clan-core-path = clanCoreWithVendoredDeps; clan-core-path = clanCoreWithVendoredDeps;
templateDerivation = templateDerivation; templateDerivation = templateDerivation;
pythonRuntime = pkgs.python3; pythonRuntime = pkgs.python3;

View File

@@ -1,6 +1,7 @@
{ {
lib, lib,
nix-unit, nix-unit,
nix-select,
clan-cli, clan-cli,
mkShell, mkShell,
ruff, ruff,
@@ -43,6 +44,7 @@ mkShell {
# Needed for impure tests # Needed for impure tests
ln -sfT ${clan-cli.nixpkgs} "$PKG_ROOT/clan_cli/nixpkgs" ln -sfT ${clan-cli.nixpkgs} "$PKG_ROOT/clan_cli/nixpkgs"
ln -sfT ${nix-select} "$PKG_ROOT/clan_cli/select"
# Generate classes.py from inventory schema # Generate classes.py from inventory schema
# This file is in .gitignore # This file is in .gitignore