Home Explore Blog Models CI



nixpkgs

2nd chunk of `pkgs/by-name/ni/nixos-rebuild-ng/README.md`
6ae0b55a34bf71a4148e1c30719627f73477d048774c59900000000100000ede
## Development

Run:

```console
nix-build -A nixos-rebuild-ng -A nixos-rebuild-ng.tests.linters
```

The command above will build, run the unit tests and linters, and also check if
the code is formatted. However, sometimes it's more convenient to run just a few
tests to debug, in this case you can run:

```console
nix-shell -A nixos-rebuild-ng.devShell
```

The command above should automatically put you inside `src` directory, and you
can run:

```console
# run program
python -m nixos_rebuild
# run tests
pytest
# check types
mypy .
# fix lint issues
ruff check --fix .
# format code
ruff format .
```

## Breaking changes

While `nixos-rebuild-ng` tries to be as much of a clone of the original as
possible, there are still some breaking changes that were done in order to
improve the user experience. If they break your workflow in some way that is
not possible to fix, please open an issue and we can discuss a solution.

- For `--build-host` and `--target-host`, `nixos-rebuild-ng` does not allocate
  a pseudo-TTY via SSH (e.g., `ssh -t`) anymore. The reason for this is that
  pseudo-TTY breaks some expectations from SSH, like it mangles stdout and
  stderr, and can
  [break terminal output](https://github.com/NixOS/nixpkgs/issues/336967) in
  some situations.
  The issue is that `sudo` needs a TTY to ask for password, otherwise it will
  fail. The solution for this is a new flag, `--ask-sudo-password`, that when
  used with `--target-host` (`--build-host` doesn't need `sudo`), will ask for
  the `sudo` password for the target host using Python's
  [getpass](https://docs.python.org/3/library/getpass.html) and forward it to
  every `sudo` request. Keep in mind that there is no check, so if you type
  your password wrong, it will fail during activation (this can be improved
  though)
- When `--build-host` and `--target-host` are used together, we will use `nix
  copy` instead of SSH'ing to build host and using
  `nix-copy-closure --to target-host`. The reason for this is documented in PR
  [#364698](https://github.com/NixOS/nixpkgs/pull/364698). If you do need the
  previous behavior, you can simulate it using `ssh build-host --
  nixos-rebuild-ng switch --target-host target-host`. If that is not the case,
  please open an issue
- We do some additional validation of flags, like exiting with an error when
  `--build-host` or `--target-host` is used with `repl`, since the user could
  assume that the `repl` would be run remotely while it always runs the local
  machine. `nixos-rebuild` silently ignored those flags, so this
  [may cause some issues](https://github.com/NixOS/nixpkgs/pull/363922) for
  wrappers

## Caveats

- Bugs in the profile manipulation can cause corruption of your profile that
  may be difficult to fix, so right now I only recommend using
  `nixos-rebuild-ng` if you are testing in a VM or in a filesystem with
  snapshots like BTRFS or ZFS. Those bugs are unlikely to be unfixable but the
  errors can be difficult to understand. If you want to go on,
  `nix-collect-garbage -d` and `nix store repair` are your friends

## TODON'T

- Nix bootstrap: it is only used for non-Flake paths and it is basically
  useless nowadays. It was created at a time when Nix was changing frequently
  and there was a need to bootstrap a new version of Nix before evaluating the
  configuration (otherwise the new Nixpkgs version may have code that is only
  compatible with a newer version of Nix). Nixpkgs now has a policy to be
  compatible with Nix 2.18, and even if this is bumped as long we don't do
  drastic minimum version changes this should not be an issue. Also, the daemon
  itself always run with the previous version since even we can replace Nix in
  `PATH` (so Nix client), but we can't replace the daemon without switching to
  a new version.

Title: nixos-rebuild-ng: Development, Breaking Changes, and Caveats
Summary
This section outlines the development process for `nixos-rebuild-ng`, detailing how to build, run tests, lint, and format the code using `nix-build` and `nix-shell` commands, as well as direct Python tooling. It then enumerates several breaking changes from the original `nixos-rebuild`, including the removal of pseudo-TTY allocation for `--build-host` and `--target-host` (introducing `--ask-sudo-password` for sudo), the switch to `nix copy` when both host flags are used, and stricter validation for incompatible flag combinations. Finally, it cautions users about potential profile corruption bugs, recommending `nixos-rebuild-ng` primarily for testing environments with snapshots, and explicitly states that Nix bootstrap functionality will not be implemented due to its perceived obsolescence.