1
0
forked from clan/clan-core

cli: fix nixpkgs unfree import

This commit is contained in:
Jörg Thalheim 2023-09-06 17:29:49 +02:00
parent 35c5f248d1
commit 3bdd3af248
7 changed files with 34 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,6 +1,6 @@
.direnv
result*
pkgs/clan-cli/clan_cli/deps_flake
pkgs/clan-cli/clan_cli/nixpkgs
pkgs/clan-cli/clan_cli/webui/assets
# python

View File

@ -6,7 +6,7 @@ from typing import Optional
from fastapi import HTTPException
from clan_cli.dirs import get_clan_flake_toplevel, nixpkgs
from clan_cli.dirs import get_clan_flake_toplevel, nixpkgs_source
from clan_cli.machines.folders import machine_folder, machine_settings_file
from clan_cli.nix import nix_eval
@ -54,7 +54,7 @@ def schema_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict:
f"""
let
flake = builtins.getFlake (toString {flake});
lib = import {nixpkgs()}/lib;
lib = import {nixpkgs_source()}/lib;
options = flake.nixosConfigurations.{machine_name}.options;
clanOptions = options.clan;
jsonschemaLib = import {Path(__file__).parent / "jsonschema"} {{ inherit lib; }};

View File

@ -1,4 +1,3 @@
import json
import os
import sys
from pathlib import Path
@ -31,15 +30,12 @@ def module_root() -> Path:
return Path(__file__).parent
def deps_flake() -> Path:
return (module_root() / "deps_flake").resolve()
def nixpkgs_flake() -> Path:
return (module_root() / "nixpkgs").resolve()
def nixpkgs() -> Path:
# load the flake.lock json file from the deps_flake and return nodes.nixpkgs.path
with open(deps_flake() / "flake.lock") as f:
flake_lock = json.load(f)
return Path(flake_lock["nodes"]["nixpkgs"]["locked"]["path"])
def nixpkgs_source() -> Path:
return (module_root() / "nixpkgs" / "path").resolve()
def unfree_nixpkgs() -> Path:

View File

@ -1,7 +1,7 @@
import os
import tempfile
from .dirs import deps_flake, nixpkgs, unfree_nixpkgs
from .dirs import nixpkgs_flake, nixpkgs_source, unfree_nixpkgs
def nix_eval(flags: list[str]) -> list[str]:
@ -15,7 +15,7 @@ def nix_eval(flags: list[str]) -> list[str]:
"nix-command flakes",
"--override-input",
"nixpkgs",
str(nixpkgs()),
str(nixpkgs_source()),
# --store is required to prevent this error:
# error: cannot unlink '/nix/store/6xg259477c90a229xwmb53pdfkn6ig3g-default-builder.sh': Operation not permitted
"--store",
@ -43,7 +43,7 @@ def nix_shell(packages: list[str], cmd: list[str]) -> list[str]:
"--extra-experimental-features",
"nix-command flakes",
"--inputs-from",
f"{str(deps_flake())}",
f"{str(nixpkgs_flake())}",
]
+ wrapped_packages
+ ["-c"]

View File

@ -44,19 +44,33 @@ let
checkPython = python3.withPackages (_ps: dependencies ++ testDependencies);
# - vendor the jsonschema nix lib (copy instead of symlink).
# - lib.cleanSource prevents unnecessary rebuilds when `self` changes.
source = runCommand "clan-cli-source" { } ''
cp -r ${./.} $out
chmod -R +w $out
rm $out/clan_cli/config/jsonschema
cp -r ${depsFlake} $out/clan_cli/deps_flake
cp -r ${nixpkgs} $out/clan_cli/nixpkgs
cp -r ${../../lib/jsonschema} $out/clan_cli/config/jsonschema
ln -s ${ui-assets} $out/clan_cli/webui/assets
'';
depsFlake = runCommand "deps-flake" { } ''
nixpkgs = runCommand "nixpkgs" { nativeBuildInputs = [ pkgs.nix ]; } ''
mkdir $out
cp ${./deps-flake.nix} $out/flake.nix
${pkgs.nix}/bin/nix flake lock $out \
mkdir -p $out/unfree
cat > $out/unfree/default.nix <<EOF
import "${pkgs.path}" { config = { allowUnfree = true; overlays = []; }; }
EOF
cat > $out/flake.nix << EOF
{
description = "dependencies for the clan-cli";
inputs = {
nixpkgs.url = "nixpkgs";
};
outputs = _inputs: { };
}
EOF
ln -s ${pkgs.path} $out/path
nix flake lock $out \
--store ./. \
--experimental-features 'nix-command flakes' \
--override-input nixpkgs ${pkgs.path}
@ -94,7 +108,7 @@ python3.pkgs.buildPythonPackage {
${checkPython}/bin/python ./bin/gen-openapi --out $out/openapi.json --app-dir . clan_cli.webui.app:app
touch $out
'';
passthru.depsFlake = depsFlake;
passthru.nixpkgs = nixpkgs;
passthru.devDependencies = [
setuptools
@ -104,7 +118,7 @@ python3.pkgs.buildPythonPackage {
passthru.testDependencies = dependencies ++ testDependencies;
postInstall = ''
cp -r ${depsFlake} $out/${python3.sitePackages}/clan_cli/deps_flake
cp -r ${nixpkgs} $out/${python3.sitePackages}/clan_cli/nixpkgs
installShellCompletion --bash --name clan \
<(${argcomplete}/bin/register-python-argcomplete --shell bash clan)
installShellCompletion --fish --name clan.fish \

View File

@ -27,7 +27,7 @@ mkShell {
tmp_path=$(realpath ./.direnv)
rm -f clan_cli/nixpkgs clan_cli/assets
ln -sf ${clan-cli.depsFlake} clan_cli/deps_flake
ln -sf ${clan-cli.nixpkgs} clan_cli/nixpkgs
ln -sf ${ui-assets} clan_cli/webui/assets
export PATH="$tmp_path/bin:${checkScript}/bin:$PATH"

View File

@ -6,7 +6,7 @@ from typing import Generator
import pytest
from clan_cli.dirs import nixpkgs
from clan_cli.dirs import nixpkgs_source
sys.path.append(os.path.join(os.path.dirname(__file__), "helpers"))
@ -44,7 +44,7 @@ def machine_flake(monkeymodule: pytest.MonkeyPatch) -> Generator[Path, None, Non
# provided by get_clan_flake_toplevel
flake_nix = flake / "flake.nix"
flake_nix.write_text(
flake_nix.read_text().replace("__NIXPKGS__", str(nixpkgs()))
flake_nix.read_text().replace("__NIXPKGS__", str(nixpkgs_source()))
)
# check that an empty config is returned if no json file exists
monkeymodule.chdir(flake)