Nixos-rebuild: Difference between revisions

Add note about using nixos-rebuild to switch to a specific generation
Mightyiam (talk | contribs)
Correct position of references and categories
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:nixos-rebuild}}{{Cleanup}}
'''nixos-rebuild''' is the NixOS command used to apply changes made to the system configuration. It can also be used for a variety of other tasks related to managing the state of a NixOS system.
'''nixos-rebuild''' is the NixOS command used to apply changes made to the system configuration. It can also be used for a variety of other tasks related to managing the state of a NixOS system.


===Basic functionality===
<code>nixos-rebuild-ng</code> is a rewrite of <code>nixos-rebuild</code> in Python instead of Bash which has better performance and more features, and will become the default in NixOS 25.11.
 
== Usage ==
NixOS follows a "declarative configuration" approach, which means that the proper way to modify your system is to make changes to your system configuration (typically <code>/etc/nixos/configuration.nix</code>), and then ''rebuild'' your system with <code>nixos-rebuild</code>:
NixOS follows a "declarative configuration" approach, which means that the proper way to modify your system is to make changes to your system configuration (typically <code>/etc/nixos/configuration.nix</code>), and then ''rebuild'' your system with <code>nixos-rebuild</code>:


Line 22: Line 26:
* <code>--upgrade</code>: Update the <code>nixos</code> channel of the root user before building the configuration.
* <code>--upgrade</code>: Update the <code>nixos</code> channel of the root user before building the configuration.


=== Deploying on other machines ===
<code>nixos-rebuild</code> can also be used to build and deploy system configurations on remote hosts via SSH. To use a remote host to build your system and deploy it on the current host, use:
<code>nixos-rebuild</code> can also be used to build and deploy system configurations on remote hosts via SSH. To use a remote host to build your system and deploy it on the current host, use:
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
# nixos-rebuild --build-host user@example.com switch
# nixos-rebuild --build-host user@example.com switch
</syntaxHighlight>
</syntaxHighlight>
Add the flag <code>--use-substitutes</code> to make the remote host use substitutes instead of copying from your host to the remote host.
To build the system locally and deploy it on a remote host, use:
To build the system locally and deploy it on a remote host, use:
<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
Line 38: Line 45:
</syntaxHighlight>
</syntaxHighlight>


To enter a password while using remote sudo, prefix the command with <code>NIX_SSHOPTS="-o RequestTTY=force"</code>.
To enter a password while using remote sudo, prefix the command with <code>NIX_SSHOPTS="-o RequestTTY=force"</code>, or add <code>--ask-sudo-password</code> with <code>nixos-rebuild-ng</code>.


{{Note|When rebuilding a remote host, you may see similar errors to the following:
{{Note|When rebuilding a remote host, you may see similar errors to the following:
Line 45: Line 52:


For a full list of sub-commands and options, see the <code>nixos-rebuild</code> man page.
For a full list of sub-commands and options, see the <code>nixos-rebuild</code> man page.
==== Deploying on a different architecture ====
There is currently a regression within <code>nixos-rebuild</code> that causes the local <code>nixos-rebuild</code> to invoke the binaries for the target architecture on the local system. This will naturally fail, throwing an ''"Exec format error".''<ref>https://github.com/NixOS/nixpkgs/issues/166499</ref> This can be bypassed by running <code>nixos-rebuild</code> with the <code>--fast</code> flag.


===Rolling back to a specific generation===
===Rolling back to a specific generation===
Line 111: Line 121:
* Add the resulting derivation to the system profile in <code>/nix/var/nix/profiles</code>, i. e. create a new generation in the system profile.
* Add the resulting derivation to the system profile in <code>/nix/var/nix/profiles</code>, i. e. create a new generation in the system profile.
* Add the new generation to the bootloader menu as the new default and activate it. If you've manually built the system derivation, this can also be done with <code>result/bin/switch-to-configuration switch</code>.
* Add the new generation to the bootloader menu as the new default and activate it. If you've manually built the system derivation, this can also be done with <code>result/bin/switch-to-configuration switch</code>.
== See also ==
* [https://nixcademy.com/posts/nixos-rebuild/ Magic Deployments with nixos-rebuild, Nixcademy]
* [https://nixcademy.com/posts/nixos-rebuild-remote-deployment/ Remote Deployments with nixos-rebuild, Nixcademy]
== References ==
<references />


[[Category:NixOS]]
[[Category:NixOS]]