Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

160 lines
4.6 KiB
Nix
Raw Permalink Normal View History

2024-11-04 22:23:33 +00:00
{
lib,
2024-12-10 10:24:18 +00:00
stdenv,
callPackage,
2024-11-04 22:23:33 +00:00
installShellFiles,
2024-11-16 21:25:22 +00:00
mkShell,
2024-11-04 22:23:33 +00:00
nix,
python3,
2024-11-20 19:41:42 +00:00
python3Packages,
runCommand,
scdoc,
2024-11-04 22:23:33 +00:00
withNgSuffix ? true,
withReexec ? false,
withShellFiles ? true,
2024-12-10 10:24:18 +00:00
# Very long tmp dirs lead to "too long for Unix domain socket"
# SSH ControlPath errors. Especially macOS sets long TMPDIR paths.
withTmpdir ? if stdenv.hostPlatform.isDarwin then "/tmp" else null,
# passthru.tests
nixosTests,
nixVersions,
lixPackageSets,
nixos-rebuild-ng,
2024-11-04 22:23:33 +00:00
}:
let
executable = if withNgSuffix then "nixos-rebuild-ng" else "nixos-rebuild";
in
2024-11-20 19:41:42 +00:00
python3Packages.buildPythonApplication rec {
2024-11-04 22:23:33 +00:00
pname = "nixos-rebuild-ng";
version = "0.0.0";
src = ./src;
pyproject = true;
2024-11-20 19:41:42 +00:00
build-system = with python3Packages; [
2024-11-04 22:23:33 +00:00
setuptools
];
nativeBuildInputs = lib.optionals withShellFiles [
installShellFiles
python3Packages.shtab
scdoc
];
2024-11-04 22:23:33 +00:00
propagatedBuildInputs = [
# Make sure that we use the Nix package we depend on, not something
# else from the PATH for nix-{env,instantiate,build}. This is
# important, because NixOS defaults the architecture of the rebuilt
# system to the architecture of the nix-* binaries used. So if on an
# amd64 system the user has an i686 Nix package in her PATH, then we
# would silently downgrade the whole system to be i686 NixOS on the
# next reboot.
# The binary will be included in the wrapper for Python.
nixos-rebuild-ng: only propagate nix's `bin` output Prior to this the `dev` output was also propagated, when it's not actually used ```console $ nix-store --query --references /nix/store/ppw0flx4dbksxsnr84hq1gz4k0s0hpcq-nixos-rebuild-ng-0.0.0 /nix/store/11ciq72n4fdv8rw6wgjgasfv4mjs1jrw-bash-5.2p37 /nix/store/26yi95240650jxp5dj78xzch70i1kzlz-python3-3.12.9 /nix/store/xxh7mivp64xmzyw5wir2c2xbhy6cjzjd-nix-2.24.12 /nix/store/8jai5cxdfzgj9nsz4i26fh9sx5zsgilz-nix-2.24.12-dev /nix/store/ppw0flx4dbksxsnr84hq1gz4k0s0hpcq-nixos-rebuild-ng-0.0.0 ``` ```console $ nix why-depends --all --precise /nix/store/ppw0flx4dbksxsnr84hq1gz4k0s0hpcq-nixos-rebuild-ng-0.0.0 /nix/store/8jai5cxdfzgj9nsz4i26fh9sx5zsgilz-nix-2.24.12-dev /nix/store/ppw0flx4dbksxsnr84hq1gz4k0s0hpcq-nixos-rebuild-ng-0.0.0 └───nix-support/propagated-build-inputs: …/nix/store/8jai5cxdfzgj9nsz4i26fh9sx5zsgilz-nix-2.24.12-dev /nix/store/26yi… → /nix/store/8jai5cxdfzgj9nsz4i26fh9sx5zsgilz-nix-2.24.12-dev ``` ```console $ nvd diff /nix/store/ppw0flx4dbksxsnr84hq1gz4k0s0hpcq-nixos-rebuild-ng-0.0.0 /nix/store/fqm81bhggzkqh7033np2z0jr8c0qrpbw-nixos-rebuild-ng-0.0.0 <<< /nix/store/ppw0flx4dbksxsnr84hq1gz4k0s0hpcq-nixos-rebuild-ng-0.0.0 >>> /nix/store/fqm81bhggzkqh7033np2z0jr8c0qrpbw-nixos-rebuild-ng-0.0.0 Version changes: [C.] #1 boehm-gc 8.2.8, 8.2.8-dev -> 8.2.8 [C*] #2 nix 2.24.12, 2.24.12-dev, 2.24.12-man -> 2.24.12, 2.24.12-man Removed packages: [R.] #1 nlohmann_json 3.11.3 Closure size: 66 -> 63 (1 paths added, 4 paths removed, delta -3, disk usage -1.9MiB). ```
2025-03-05 06:05:02 -05:00
(lib.getBin nix)
2024-11-04 22:23:33 +00:00
];
postPatch = ''
substituteInPlace nixos_rebuild/constants.py \
--subst-var-by executable ${executable} \
--subst-var-by withReexec ${lib.boolToString withReexec} \
--subst-var-by withShellFiles ${lib.boolToString withShellFiles}
substituteInPlace pyproject.toml \
2024-12-07 11:38:03 +00:00
--replace-fail nixos-rebuild ${executable}
'';
postInstall = lib.optionalString withShellFiles ''
scdoc < ${./nixos-rebuild.8.scd} > ${executable}.8
installManPage ${executable}.8
installShellCompletion --cmd ${executable} \
--bash <(shtab --shell bash nixos_rebuild.get_main_parser) \
--zsh <(shtab --shell zsh nixos_rebuild.get_main_parser)
'';
2024-11-04 22:23:33 +00:00
2024-11-20 19:41:42 +00:00
nativeCheckInputs = with python3Packages; [
2024-11-04 22:23:33 +00:00
pytestCheckHook
];
pytestFlags = [ "-vv" ];
2024-11-04 22:23:33 +00:00
2024-12-10 10:24:18 +00:00
makeWrapperArgs = lib.optionals (withTmpdir != null) [
"--set TMPDIR ${withTmpdir}"
];
2024-11-16 21:25:22 +00:00
passthru =
let
python-with-pkgs = python3.withPackages (
ps: with ps; [
mypy
pytest
# this is to help development (e.g.: better diffs) inside devShell
# only, do not use its helpers like `mocker`
2025-02-13 21:27:19 +00:00
pytest-mock
2024-11-16 21:25:22 +00:00
ruff
]
);
in
{
devShell = mkShell {
packages = [ python-with-pkgs ];
shellHook = ''
cd pkgs/by-name/ni/nixos-rebuild-ng/src || true
'';
};
tests = {
with_reexec = nixos-rebuild-ng.override {
withReexec = true;
withNgSuffix = false;
};
with_nix_latest = nixos-rebuild-ng.override {
nix = nixVersions.latest;
};
with_nix_stable = nixos-rebuild-ng.override {
nix = nixVersions.stable;
};
with_nix_2_28 = nixos-rebuild-ng.override {
# oldest supported version in nixpkgs
nix = nixVersions.nix_2_28;
};
with_lix_latest = nixos-rebuild-ng.override {
nix = lixPackageSets.latest.lix;
};
with_lix_stable = nixos-rebuild-ng.override {
nix = lixPackageSets.stable.lix;
};
inherit (nixosTests)
# FIXME: this test is disabled since it times out in @ofborg
# nixos-rebuild-install-bootloader-ng
nixos-rebuild-specialisations-ng
nixos-rebuild-target-host-ng
;
repl = callPackage ./tests/repl.nix { };
# NOTE: this is a passthru test rather than a build-time test because we
# want to keep the build closures small
linters = runCommand "${pname}-linters" { nativeBuildInputs = [ python-with-pkgs ]; } ''
export RUFF_CACHE_DIR="$(mktemp -d)"
echo -e "\x1b[32m## run mypy\x1b[0m"
mypy ${src}
echo -e "\x1b[32m## run ruff\x1b[0m"
ruff check ${src}
echo -e "\x1b[32m## run ruff format\x1b[0m"
ruff format --check ${src}
2024-11-16 21:25:22 +00:00
touch $out
'';
};
2024-11-16 21:25:22 +00:00
};
2024-11-04 22:23:33 +00:00
meta = {
description = "Rebuild your NixOS configuration and switch to it, on local hosts and remote";
homepage = "https://github.com/NixOS/nixpkgs/tree/master/pkgs/by-name/ni/nixos-rebuild-ng";
license = lib.licenses.mit;
maintainers = [ ];
teams = [ lib.teams.nixos-rebuild ];
mainProgram = executable;
2024-11-04 22:23:33 +00:00
};
}