···53535454And use `nixos-rebuild-ng` instead of `nixos-rebuild`.
55555656+## Development
5757+5858+Run:
5959+6060+```console
6161+nix-build -A nixos-rebuild-ng.tests.ci
6262+```
6363+6464+The command above will run the unit tests and linters, and also check if the
6565+code is formatted. However, sometimes is more convenient to run just a few
6666+tests to debug, in this case you can run:
6767+6868+```console
6969+nix-shell -A nixos-rebuild-ng.devShell
7070+```
7171+7272+The command above should automatically put you inside `src` directory, and you
7373+can run:
7474+7575+```console
7676+# run program
7777+python -m nixos_rebuild
7878+# run tests
7979+python -m pytest
8080+# check types
8181+mypy .
8282+# fix lint issues
8383+ruff check --fix .
8484+# format code
8585+ruff format .
8686+```
8787+5688## Current caveats
57895890- For now we will install it in `nixos-rebuild-ng` path by default, to avoid
···96128- [ ] Improve documentation
97129- [ ] `nixos-rebuild repl` (calling old `nixos-rebuild` for now)
98130- [ ] `nix` build/bootstrap
9999-- [ ] Reduce build closure
131131+- [ ] Generate tab completion via [`shtab`](https://docs.iterative.ai/shtab/)
132132+- [x] Reduce build closure
133133+134134+## TODON'T
135135+136136+- Reimplement `systemd-run` logic (will be moved to the new
137137+ [`apply`](https://github.com/NixOS/nixpkgs/pull/344407) script)
+43-15
pkgs/by-name/ni/nixos-rebuild-ng/package.nix
···11{
22 lib,
33 installShellFiles,
44+ mkShell,
45 nix,
56 nixos-rebuild,
67 python3,
88+ python3Packages,
99+ runCommand,
710 withNgSuffix ? true,
811}:
99-python3.pkgs.buildPythonApplication {
1212+python3Packages.buildPythonApplication rec {
1013 pname = "nixos-rebuild-ng";
1114 version = "0.0.0";
1215 src = ./src;
1316 pyproject = true;
14171515- build-system = with python3.pkgs; [
1818+ build-system = with python3Packages; [
1619 setuptools
1720 ];
18211919- dependencies = with python3.pkgs; [
2222+ dependencies = with python3Packages; [
2023 tabulate
2121- types-tabulate
2224 ];
23252426 nativeBuildInputs = [
···5355 mv $out/bin/nixos-rebuild $out/bin/nixos-rebuild-ng
5456 '';
55575656- nativeCheckInputs = with python3.pkgs; [
5858+ nativeCheckInputs = with python3Packages; [
5759 pytestCheckHook
5858- mypy
5959- ruff
6060 ];
61616262 pytestFlagsArray = [ "-vv" ];
63636464- postCheck = ''
6565- echo -e "\x1b[32m## run mypy\x1b[0m"
6666- mypy nixos_rebuild tests
6767- echo -e "\x1b[32m## run ruff\x1b[0m"
6868- ruff check nixos_rebuild tests
6969- echo -e "\x1b[32m## run ruff format\x1b[0m"
7070- ruff format --check nixos_rebuild tests
7171- '';
6464+ passthru =
6565+ let
6666+ python-with-pkgs = python3.withPackages (
6767+ ps: with ps; [
6868+ mypy
6969+ pytest
7070+ ruff
7171+ types-tabulate
7272+ # dependencies
7373+ tabulate
7474+ ]
7575+ );
7676+ in
7777+ {
7878+ devShell = mkShell {
7979+ packages = [ python-with-pkgs ];
8080+ shellHook = ''
8181+ cd pkgs/by-name/ni/nixos-rebuild-ng/src || true
8282+ '';
8383+ };
8484+8585+ # NOTE: this is a passthru test rather than a build-time test because we
8686+ # want to keep the build closures small
8787+ tests.ci = runCommand "${pname}-ci" { nativeBuildInputs = [ python-with-pkgs ]; } ''
8888+ export RUFF_CACHE_DIR="$(mktemp -d)"
8989+9090+ echo -e "\x1b[32m## run mypy\x1b[0m"
9191+ mypy ${src}
9292+ echo -e "\x1b[32m## run ruff\x1b[0m"
9393+ ruff check ${src}
9494+ echo -e "\x1b[32m## run ruff format\x1b[0m"
9595+ ruff format --check ${src}
9696+9797+ touch $out
9898+ '';
9999+ };
7210073101 meta = {
74102 description = "Rebuild your NixOS configuration and switch to it, on local hosts and remote";
···77from subprocess import run
88from typing import assert_never
991010-from tabulate import tabulate
1111-1210from .models import Action, Flake, NRError, Profile
1311from .nix import (
1412 edit,
1313+ find_file,
1414+ get_nixpkgs_rev,
1515 list_generations,
1616 nixos_build,
1717 nixos_build_flake,
···8888 if args.upgrade or args.upgrade_all:
8989 upgrade_channels(bool(args.upgrade_all))
90909191- match action := Action(args.action):
9191+ action = Action(args.action)
9292+ # Only run shell scripts from the Nixpkgs tree if the action is
9393+ # "switch", "boot", or "test". With other actions (such as "build"),
9494+ # the user may reasonably expect that no code from the Nixpkgs tree is
9595+ # executed, so it's safe to run nixos-rebuild against a potentially
9696+ # untrusted tree.
9797+ can_run = action in (Action.SWITCH, Action.BOOT, Action.TEST)
9898+ if can_run and not flake:
9999+ nixpkgs_path = find_file("nixpkgs", nix_flags)
100100+ rev = get_nixpkgs_rev(nixpkgs_path)
101101+ if nixpkgs_path and rev:
102102+ (nixpkgs_path / ".version-suffix").write_text(rev)
103103+104104+ match action:
92105 case Action.SWITCH | Action.BOOT:
93106 info("building the system configuration...")
94107 if args.rollback:
···177190 if args.json:
178191 print(json.dumps(generations, indent=2))
179192 else:
193193+ from tabulate import tabulate
194194+180195 headers = {
181196 "generation": "Generation",
182197 "date": "Build-date",
···216231 raise ex
217232 else:
218233 sys.exit(str(ex))
219219-220220-221221-if __name__ == "__main__":
222222- main()