2024-11-04 22:23:33 +00:00
|
|
|
{
|
|
|
|
lib,
|
2024-12-10 10:24:18 +00:00
|
|
|
stdenv,
|
2024-12-09 14:34:25 +00:00
|
|
|
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,
|
2024-11-16 10:49:48 +00:00
|
|
|
runCommand,
|
2024-12-04 12:52:02 +00:00
|
|
|
scdoc,
|
2024-11-04 22:23:33 +00:00
|
|
|
withNgSuffix ? true,
|
2024-12-09 12:10:13 +00:00
|
|
|
withReexec ? false,
|
2024-12-05 12:56:59 +00:00
|
|
|
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,
|
2025-06-21 01:01:32 +02:00
|
|
|
# passthru.tests
|
|
|
|
nixosTests,
|
|
|
|
nixVersions,
|
2025-06-21 12:06:23 +02:00
|
|
|
lixPackageSets,
|
2025-06-21 01:01:32 +02:00
|
|
|
nixos-rebuild-ng,
|
2024-11-04 22:23:33 +00:00
|
|
|
}:
|
2024-12-04 12:32:13 +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
|
|
|
|
];
|
|
|
|
|
2024-12-05 10:25:43 +00:00
|
|
|
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.
|
2025-03-05 06:05:02 -05:00
|
|
|
(lib.getBin nix)
|
2024-11-04 22:23:33 +00:00
|
|
|
];
|
|
|
|
|
2024-12-04 12:52:02 +00:00
|
|
|
postPatch = ''
|
nixos-rebuild-ng: change copy closure logic when copying from_host -> to_host
- If user is using Nix 2.18+ (the default), we will switch to use `nix
copy` since it supports using `--from` and `--to` at the same time
- If user is using older versions of Nix, we will call
`nix-copy-closure` twice, once with `--from` and another with `--to`
The reason for this change is because the previous logic, that SSH'd to
from_host to copy to to_host using `nix-copy-closure --to` means that
`from_host` needs to have credentials to communicate with `to_host`,
that is not always possible nor expected. It breaks even in simple cases
like `--from` and `--to` is the same host because it is not common for a
host to SSH'd itself. This also makes the behavior more consistent,
since `--from` and `--to` is interpreted from the eval host point of
view EXCEPT when they're used together.
This may of course break in the cases where this old behavior was
assumed, e.g.: deploying with a bastion host. I am not sure how common
this was, and I think this change in behavior should enable more cases
than break, but let's see. For that particular case, running
`nixos-rebuild-ng` inside SSH command (e.g.: `ssh build_host --
nixos-rebuild-ng switch --target-host target_host`) should do the
trick.
2024-12-12 17:33:40 +00:00
|
|
|
substituteInPlace nixos_rebuild/constants.py \
|
2024-12-06 10:56:00 +00:00
|
|
|
--subst-var-by executable ${executable} \
|
2024-12-09 12:10:13 +00:00
|
|
|
--subst-var-by withReexec ${lib.boolToString withReexec} \
|
2024-12-06 10:56:00 +00:00
|
|
|
--subst-var-by withShellFiles ${lib.boolToString withShellFiles}
|
|
|
|
|
2024-12-05 10:25:43 +00:00
|
|
|
substituteInPlace pyproject.toml \
|
2024-12-07 11:38:03 +00:00
|
|
|
--replace-fail nixos-rebuild ${executable}
|
2024-12-04 12:52:02 +00:00
|
|
|
'';
|
|
|
|
|
2024-12-05 10:25:43 +00:00
|
|
|
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
|
|
|
|
];
|
|
|
|
|
2025-05-01 03:46:26 +08:00
|
|
|
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
|
2025-02-13 21:30:17 +00:00
|
|
|
# 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
|
|
|
|
'';
|
|
|
|
};
|
2024-11-16 10:49:48 +00:00
|
|
|
|
2024-12-09 14:34:25 +00:00
|
|
|
tests = {
|
2025-06-27 11:45:21 +01:00
|
|
|
with_reexec = nixos-rebuild-ng.override {
|
|
|
|
withReexec = true;
|
|
|
|
withNgSuffix = false;
|
|
|
|
};
|
2025-06-21 01:01:32 +02:00
|
|
|
with_nix_latest = nixos-rebuild-ng.override {
|
|
|
|
nix = nixVersions.latest;
|
|
|
|
};
|
|
|
|
with_nix_stable = nixos-rebuild-ng.override {
|
|
|
|
nix = nixVersions.stable;
|
|
|
|
};
|
2025-08-14 10:46:01 +02:00
|
|
|
with_nix_2_28 = nixos-rebuild-ng.override {
|
2025-07-24 16:08:14 +02:00
|
|
|
# oldest supported version in nixpkgs
|
2025-08-14 10:46:01 +02:00
|
|
|
nix = nixVersions.nix_2_28;
|
2025-06-21 01:01:32 +02:00
|
|
|
};
|
2025-06-21 12:06:23 +02:00
|
|
|
with_lix_latest = nixos-rebuild-ng.override {
|
|
|
|
nix = lixPackageSets.latest.lix;
|
|
|
|
};
|
|
|
|
with_lix_stable = nixos-rebuild-ng.override {
|
|
|
|
nix = lixPackageSets.stable.lix;
|
|
|
|
};
|
2025-06-21 01:01:32 +02:00
|
|
|
|
2024-12-10 12:50:12 +00:00
|
|
|
inherit (nixosTests)
|
2025-06-28 21:13:50 +01:00
|
|
|
# FIXME: this test is disabled since it times out in @ofborg
|
|
|
|
# nixos-rebuild-install-bootloader-ng
|
2024-12-10 12:50:12 +00:00
|
|
|
nixos-rebuild-specialisations-ng
|
|
|
|
nixos-rebuild-target-host-ng
|
|
|
|
;
|
2024-12-09 14:34:25 +00:00
|
|
|
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)"
|
2024-11-16 10:49:48 +00:00
|
|
|
|
2024-12-09 14:34:25 +00:00
|
|
|
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
|
|
|
|
2024-12-09 14:34:25 +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;
|
2025-06-10 18:47:48 +01:00
|
|
|
maintainers = [ ];
|
|
|
|
teams = [ lib.teams.nixos-rebuild ];
|
2024-12-04 12:32:13 +00:00
|
|
|
mainProgram = executable;
|
2024-11-04 22:23:33 +00:00
|
|
|
};
|
|
|
|
}
|