···5354And use `nixos-rebuild-ng` instead of `nixos-rebuild`.
550000000000000000000000000000000056## Current caveats
5758- For now we will install it in `nixos-rebuild-ng` path by default, to avoid
···96- [ ] Improve documentation
97- [ ] `nixos-rebuild repl` (calling old `nixos-rebuild` for now)
98- [ ] `nix` build/bootstrap
99-- [ ] Reduce build closure
000000
···5354And use `nixos-rebuild-ng` instead of `nixos-rebuild`.
5556+## Development
57+58+Run:
59+60+```console
61+nix-build -A nixos-rebuild-ng.tests.ci
62+```
63+64+The command above will run the unit tests and linters, and also check if the
65+code is formatted. However, sometimes is more convenient to run just a few
66+tests to debug, in this case you can run:
67+68+```console
69+nix-shell -A nixos-rebuild-ng.devShell
70+```
71+72+The command above should automatically put you inside `src` directory, and you
73+can run:
74+75+```console
76+# run program
77+python -m nixos_rebuild
78+# run tests
79+python -m pytest
80+# check types
81+mypy .
82+# fix lint issues
83+ruff check --fix .
84+# format code
85+ruff format .
86+```
87+88## Current caveats
8990- For now we will install it in `nixos-rebuild-ng` path by default, to avoid
···128- [ ] Improve documentation
129- [ ] `nixos-rebuild repl` (calling old `nixos-rebuild` for now)
130- [ ] `nix` build/bootstrap
131+- [ ] Generate tab completion via [`shtab`](https://docs.iterative.ai/shtab/)
132+- [x] Reduce build closure
133+134+## TODON'T
135+136+- Reimplement `systemd-run` logic (will be moved to the new
137+ [`apply`](https://github.com/NixOS/nixpkgs/pull/344407) script)
+43-15
pkgs/by-name/ni/nixos-rebuild-ng/package.nix
···1{
2 lib,
3 installShellFiles,
04 nix,
5 nixos-rebuild,
6 python3,
007 withNgSuffix ? true,
8}:
9-python3.pkgs.buildPythonApplication {
10 pname = "nixos-rebuild-ng";
11 version = "0.0.0";
12 src = ./src;
13 pyproject = true;
1415- build-system = with python3.pkgs; [
16 setuptools
17 ];
1819- dependencies = with python3.pkgs; [
20 tabulate
21- types-tabulate
22 ];
2324 nativeBuildInputs = [
···53 mv $out/bin/nixos-rebuild $out/bin/nixos-rebuild-ng
54 '';
5556- nativeCheckInputs = with python3.pkgs; [
57 pytestCheckHook
58- mypy
59- ruff
60 ];
6162 pytestFlagsArray = [ "-vv" ];
6364- postCheck = ''
65- echo -e "\x1b[32m## run mypy\x1b[0m"
66- mypy nixos_rebuild tests
67- echo -e "\x1b[32m## run ruff\x1b[0m"
68- ruff check nixos_rebuild tests
69- echo -e "\x1b[32m## run ruff format\x1b[0m"
70- ruff format --check nixos_rebuild tests
71- '';
00000000000000000000000000007273 meta = {
74 description = "Rebuild your NixOS configuration and switch to it, on local hosts and remote";
···1{
2 lib,
3 installShellFiles,
4+ mkShell,
5 nix,
6 nixos-rebuild,
7 python3,
8+ python3Packages,
9+ runCommand,
10 withNgSuffix ? true,
11}:
12+python3Packages.buildPythonApplication rec {
13 pname = "nixos-rebuild-ng";
14 version = "0.0.0";
15 src = ./src;
16 pyproject = true;
1718+ build-system = with python3Packages; [
19 setuptools
20 ];
2122+ dependencies = with python3Packages; [
23 tabulate
024 ];
2526 nativeBuildInputs = [
···55 mv $out/bin/nixos-rebuild $out/bin/nixos-rebuild-ng
56 '';
5758+ nativeCheckInputs = with python3Packages; [
59 pytestCheckHook
0060 ];
6162 pytestFlagsArray = [ "-vv" ];
6364+ passthru =
65+ let
66+ python-with-pkgs = python3.withPackages (
67+ ps: with ps; [
68+ mypy
69+ pytest
70+ ruff
71+ types-tabulate
72+ # dependencies
73+ tabulate
74+ ]
75+ );
76+ in
77+ {
78+ devShell = mkShell {
79+ packages = [ python-with-pkgs ];
80+ shellHook = ''
81+ cd pkgs/by-name/ni/nixos-rebuild-ng/src || true
82+ '';
83+ };
84+85+ # NOTE: this is a passthru test rather than a build-time test because we
86+ # want to keep the build closures small
87+ tests.ci = runCommand "${pname}-ci" { nativeBuildInputs = [ python-with-pkgs ]; } ''
88+ export RUFF_CACHE_DIR="$(mktemp -d)"
89+90+ echo -e "\x1b[32m## run mypy\x1b[0m"
91+ mypy ${src}
92+ echo -e "\x1b[32m## run ruff\x1b[0m"
93+ ruff check ${src}
94+ echo -e "\x1b[32m## run ruff format\x1b[0m"
95+ ruff format --check ${src}
96+97+ touch $out
98+ '';
99+ };
100101 meta = {
102 description = "Rebuild your NixOS configuration and switch to it, on local hosts and remote";
···7from subprocess import run
8from typing import assert_never
910-from tabulate import tabulate
11-12from .models import Action, Flake, NRError, Profile
13from .nix import (
14 edit,
0015 list_generations,
16 nixos_build,
17 nixos_build_flake,
···88 if args.upgrade or args.upgrade_all:
89 upgrade_channels(bool(args.upgrade_all))
9091- match action := Action(args.action):
000000000000092 case Action.SWITCH | Action.BOOT:
93 info("building the system configuration...")
94 if args.rollback:
···177 if args.json:
178 print(json.dumps(generations, indent=2))
179 else:
00180 headers = {
181 "generation": "Generation",
182 "date": "Build-date",
···216 raise ex
217 else:
218 sys.exit(str(ex))
219-220-221-if __name__ == "__main__":
222- main()
···7from subprocess import run
8from typing import assert_never
90010from .models import Action, Flake, NRError, Profile
11from .nix import (
12 edit,
13+ find_file,
14+ get_nixpkgs_rev,
15 list_generations,
16 nixos_build,
17 nixos_build_flake,
···88 if args.upgrade or args.upgrade_all:
89 upgrade_channels(bool(args.upgrade_all))
9091+ action = Action(args.action)
92+ # Only run shell scripts from the Nixpkgs tree if the action is
93+ # "switch", "boot", or "test". With other actions (such as "build"),
94+ # the user may reasonably expect that no code from the Nixpkgs tree is
95+ # executed, so it's safe to run nixos-rebuild against a potentially
96+ # untrusted tree.
97+ can_run = action in (Action.SWITCH, Action.BOOT, Action.TEST)
98+ if can_run and not flake:
99+ nixpkgs_path = find_file("nixpkgs", nix_flags)
100+ rev = get_nixpkgs_rev(nixpkgs_path)
101+ if nixpkgs_path and rev:
102+ (nixpkgs_path / ".version-suffix").write_text(rev)
103+104+ match action:
105 case Action.SWITCH | Action.BOOT:
106 info("building the system configuration...")
107 if args.rollback:
···190 if args.json:
191 print(json.dumps(generations, indent=2))
192 else:
193+ from tabulate import tabulate
194+195 headers = {
196 "generation": "Generation",
197 "date": "Build-date",
···231 raise ex
232 else:
233 sys.exit(str(ex))
0000