clan-core/pkgs/clan-cli/clan_cli/nix.py

82 lines
2.0 KiB
Python

import os
import tempfile
from .dirs import nixpkgs_flake, nixpkgs_source, unfree_nixpkgs
def nix_build(
flags: list[str],
) -> list[str]:
return [
"nix",
"build",
"--no-link",
"--print-out-paths",
] + flags
def nix_eval(flags: list[str]) -> list[str]:
default_flags = [
"nix",
"eval",
"--show-trace",
"--json",
"--extra-experimental-features",
"nix-command flakes",
]
if os.environ.get("IN_NIX_SANDBOX"):
with tempfile.TemporaryDirectory() as nix_store:
return (
default_flags
+ [
"--override-input",
"nixpkgs",
str(nixpkgs_source()),
# --store is required to prevent this error:
# error: cannot unlink '/nix/store/6xg259477c90a229xwmb53pdfkn6ig3g-default-builder.sh': Operation not permitted
"--store",
nix_store,
]
+ flags
)
return default_flags + flags
def nix_shell(packages: list[str], cmd: list[str]) -> list[str]:
# we cannot use nix-shell inside the nix sandbox
# in our tests we just make sure we have all the packages
if os.environ.get("IN_NIX_SANDBOX"):
return cmd
wrapped_packages = [f"nixpkgs#{p}" for p in packages]
return (
[
"nix",
"shell",
"--extra-experimental-features",
"nix-command flakes",
"--inputs-from",
f"{str(nixpkgs_flake())}",
]
+ wrapped_packages
+ ["-c"]
+ cmd
)
def unfree_nix_shell(packages: list[str], cmd: list[str]) -> list[str]:
if os.environ.get("IN_NIX_SANDBOX"):
return cmd
return (
[
"nix",
"shell",
"--extra-experimental-features",
"nix-command flakes",
"-f",
str(unfree_nixpkgs()),
]
+ packages
+ ["-c"]
+ cmd
)