Ubuntu vs. NixOS: Difference between revisions
imported>Fricklerhandwerk m Fricklerhandwerk moved page Ubungu vs. NixOS to Ubuntu vs. NixOS: late at night typo |
formatting and style fixes :-) |
||
| (8 intermediate revisions by 7 users not shown) | |||
| Line 1: | Line 1: | ||
<!-- TODO''': Provide well-commented sample configuration.nix and ~/.nixpkgs/config.nix files with examples of common tasks. --> | |||
<div class="table-responsive"> | <div class="table-responsive"> | ||
{| class="wikitable" | {| class="wikitable" | ||
| Line 36: | Line 32: | ||
|Where are packages installed? | |Where are packages installed? | ||
|apt installs globally into /bin/, /usr/, etc. | |apt installs globally into /bin/, /usr/, etc. | ||
|System-wide packages are in /run/current-system/sw/ (these are installed because of /etc/nixos/configuration.nix) and /nix/var/nix/profiles/default/bin/ (this is the profile managed by root). Note that the files are just symlinks to the real packages managed by nix /nix/store/. | |System-wide packages are in /run/current-system/sw/ (these are installed because of /etc/nixos/configuration.nix) and /nix/var/nix/profiles/default/bin/ (this is the profile managed by root). Note that the files are just symlinks to the real packages managed by nix in /nix/store/. | ||
|User packages are in ~/.nix-profile/. Note that the files are just symlinks to the real packages managed by nix in /nix/store/. | |User packages are in ~/.nix-profile/. Note that the files are just symlinks to the real packages managed by nix in /nix/store/. | ||
|- | |- | ||
| Line 92: | Line 88: | ||
|- | |- | ||
|Uninstall a package | |Uninstall a package | ||
|<syntaxhighlight lang=" | |<syntaxhighlight lang="console">$ sudo apt-get remove emacs</syntaxhighlight> | ||
|remove from /etc/nixos/configuration.nix | |remove from /etc/nixos/configuration.nix | ||
<syntaxhighlight lang="console">$ sudo nixos-rebuild switch</syntaxhighlight> | <syntaxhighlight lang="console">$ sudo nixos-rebuild switch</syntaxhighlight> | ||
| Line 106: | Line 102: | ||
|<syntaxhighlight lang="console">$ sudo apt-get update</syntaxhighlight> | |<syntaxhighlight lang="console">$ sudo apt-get update</syntaxhighlight> | ||
|<syntaxhighlight lang="console">$ sudo nix-channel --update</syntaxhighlight> | |<syntaxhighlight lang="console">$ sudo nix-channel --update</syntaxhighlight> | ||
|<syntaxhighlight lang="console">$nix-channel --update</syntaxhighlight> | |<syntaxhighlight lang="console">$ nix-channel --update</syntaxhighlight> | ||
|- | |- | ||
|Upgrade packages | |Upgrade packages | ||
| Line 125: | Line 121: | ||
<syntaxhighlight lang="console">$ nix-store -q --tree /nix/var/nix/profiles/system</syntaxhighlight> | <syntaxhighlight lang="console">$ nix-store -q --tree /nix/var/nix/profiles/system</syntaxhighlight> | ||
(/run/current-system and /nix/var/nix/profiles/system are symbolic links that eventually end up at the same place.) | (/run/current-system and /nix/var/nix/profiles/system are symbolic links that eventually end up at the same place.) | ||
|<syntaxhighlight lang="console">$ nix-store --query --references\ | |<syntaxhighlight lang="console">$ nix-store --query --references \ | ||
$(nix-instantiate '<nixpkgs>' -A emacs)</syntaxhighlight> | $(nix-instantiate '<nixpkgs>' -A emacs)</syntaxhighlight> | ||
For installed packages: | For installed packages: | ||
| Line 148: | Line 144: | ||
|Select major version and stable/unstable | |Select major version and stable/unstable | ||
|Change sources.list and apt-get dist-upgrade. A an extremely infrequent and destructive operation. The nix variants are safe and easy to use. | |Change sources.list and apt-get dist-upgrade. A an extremely infrequent and destructive operation. The nix variants are safe and easy to use. | ||
|<syntaxhighlight lang="console">$ nix-channel --add\ | |<syntaxhighlight lang="console">$ nix-channel --add \ | ||
https://nixos.org | https://channels.nixos.org/nixpkgs-unstable <name></syntaxhighlight> | ||
Add the unstable channel. At that address you will find names for other versions and variants. Name can be any string. | Add the unstable channel. At that address you will find names for other versions and variants. Name can be any string. | ||
<syntaxhighlight lang="console">$ nix-channel --remove <name></syntaxhighlight> | <syntaxhighlight lang="console">$ nix-channel --remove <name></syntaxhighlight> | ||
| Line 164: | Line 160: | ||
|Install a particular version of a package | |Install a particular version of a package | ||
|<syntaxhighlight lang="console">$ apt-get install package=version</syntaxhighlight> | |<syntaxhighlight lang="console">$ apt-get install package=version</syntaxhighlight> | ||
|Although Nix on its own doesn't understand the concept of package versioning, you can install and play with older (or newer!) software via | |Although Nix on its own doesn't understand the concept of package versioning, you can install and play with older (or newer!) software via [[FAQ/Pinning Nixpkgs]] with https://lazamar.co.uk/nix-versions. | ||
| Line 171: | Line 167: | ||
$ nix-shell \ | $ nix-shell \ | ||
-p vim \ | -p vim \ | ||
-I nixpkgs= | -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/4bba6650a6a5a2009e25bdeed8c1e871601a9bfb.tar.gz | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| | | | ||
| Line 189: | Line 185: | ||
|Find packages | |Find packages | ||
|<syntaxhighlight lang="console">$ apt-cache search emacs</syntaxhighlight> | |<syntaxhighlight lang="console">$ apt-cache search emacs</syntaxhighlight> | ||
|<syntaxhighlight lang="console">$ nix-env -qaP '.*emacs.*'</syntaxhighlight> or <syntaxhighlight lang="console">$ nix search emacs</syntaxhighlight> | |<syntaxhighlight lang="console">$ nix-env -qaP '.*emacs.*'</syntaxhighlight> or <syntaxhighlight lang="console">$ nix search nixpkgs emacs</syntaxhighlight> | ||
|<syntaxhighlight lang="console">$ nix-env -qaP '.*emacs.*'</syntaxhighlight> or <syntaxhighlight lang="console">$ nix search emacs</syntaxhighlight> | |<syntaxhighlight lang="console">$ nix-env -qaP '.*emacs.*'</syntaxhighlight> or <syntaxhighlight lang="console">$ nix search nixpkgs emacs</syntaxhighlight> | ||
|- | |- | ||
|Show package description | |Show package description | ||
| Line 202: | Line 198: | ||
/nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4/bin/emacs-24.4</syntaxhighlight> | /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4/bin/emacs-24.4</syntaxhighlight> | ||
then | then | ||
<syntaxhighlight lang="console">$du -a /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4</syntaxhighlight> | <syntaxhighlight lang="console">$ du -a /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4</syntaxhighlight> | ||
|<syntaxhighlight lang="console"></syntaxhighlight> | |<syntaxhighlight lang="console"></syntaxhighlight> | ||
|- | |- | ||
|Show package for file | |Show package for file | ||
|<syntaxhighlight lang="console">$ dpkg -S /usr/bin/emacs</syntaxhighlight> | |<syntaxhighlight lang="console">$ dpkg -S /usr/bin/emacs</syntaxhighlight> | ||
|follow the symlink or <syntaxhighlight lang="console">nix-locate /bin/emacs</syntaxhighlight> (requires <syntaxhighlight lang="console">nix-index</syntaxhighlight> package) | |follow the symlink or <syntaxhighlight lang="console">$ nix-locate /bin/emacs</syntaxhighlight> (requires <syntaxhighlight lang="console">$ nix-index</syntaxhighlight> package) | ||
|(same) | |(same) | ||
|- | |- | ||
| Line 224: | Line 220: | ||
| Enable a service | | Enable a service | ||
| <syntaxhighlight lang="console">$ sudo systemctl enable apache</syntaxhighlight> | | <syntaxhighlight lang="console">$ sudo systemctl enable apache</syntaxhighlight> | ||
| In /etc/nixos/configuration.nix, add <syntaxhighlight lang=" | | In /etc/nixos/configuration.nix, add <syntaxhighlight lang="nix">services.tor.enable = true;</syntaxhighlight>, then run <syntaxhighlight lang="console">$ sudo nixos-rebuild switch</syntaxhighlight> | ||
|- | |- | ||
| Disable a service | | Disable a service | ||
| <syntaxhighlight lang="console">$ sudo systemctl disable apache</syntaxhighlight> | | <syntaxhighlight lang="console">$ sudo systemctl disable apache</syntaxhighlight> | ||
| In /etc/nixos/configuration.nix, add <syntaxhighlight lang=" | | In /etc/nixos/configuration.nix, add <syntaxhighlight lang="nix">services.tor.enable = false;</syntaxhighlight>, then run <syntaxhighlight lang="console">$ sudo nixos-rebuild switch</syntaxhighlight> | ||
|- | |- | ||
|Where your log files live | |Where your log files live | ||
| Line 237: | Line 233: | ||
|Adding a user | |Adding a user | ||
|<syntaxhighlight lang="console">$ sudo adduser alice</syntaxhighlight> | |<syntaxhighlight lang="console">$ sudo adduser alice</syntaxhighlight> | ||
|Add <syntaxhighlight lang="nix">users.users.alice = | |Add <syntaxhighlight lang="nix">users.users.alice = { | ||
isNormalUser = true; | |||
home = "/home/alice"; | |||
description = "Alice Foobar"; | |||
extraGroups = [ "wheel" "networkmanager" ]; | |||
openssh.authorizedKeys.keys = | |||
[ "ssh-dss AAAAB3Nza... alice@foobar" ]; | |||
};</syntaxhighlight> to /etc/nixos/configuration.nix and then call <syntaxhighlight lang="console">$ nixos-rebuild switch</syntaxhighlight> | |||
| | | | ||
|- | |- | ||
| Line 251: | Line 247: | ||
|List binaries | |List binaries | ||
|<syntaxhighlight lang="console">$ ls /usr/bin/</syntaxhighlight> | |<syntaxhighlight lang="console">$ ls /usr/bin/</syntaxhighlight> | ||
|<syntaxhighlight lang="console">$ ls /run/current-system/sw/bin &&\ | |<syntaxhighlight lang="console">$ ls /run/current-system/sw/bin && \ | ||
ls /nix/var/nix/profiles/default/bin/</syntaxhighlight> | ls /nix/var/nix/profiles/default/bin/</syntaxhighlight> | ||
|<syntaxhighlight lang="console">$ ls ~/.nix-profile/bin</syntaxhighlight> | |<syntaxhighlight lang="console">$ ls ~/.nix-profile/bin</syntaxhighlight> | ||
|- | |- | ||
| Line 261: | Line 257: | ||
|- | |- | ||
|Get sources for a package | |Get sources for a package | ||
|<syntaxhighlight lang=" | |<syntaxhighlight lang="console">$ sudo apt-get source emacs</syntaxhighlight> | ||
| | | | ||
|In Debian, apt-get source gets both the patched upstream source and the recipe for the package. Those need two steps in Nix. | |In Debian, apt-get source gets both the patched upstream source and the recipe for the package. Those need two steps in Nix. | ||
To find the package's attribute path: <syntaxhighlight lang="console">$ nix-env -qaP emacs</syntaxhighlight> or <syntaxhighlight lang="console">$ nox emacs</syntaxhighlight> | To find the package's attribute path: <syntaxhighlight lang="console">$ nix-env -qaP emacs</syntaxhighlight> or <syntaxhighlight lang="console">$ nox emacs</syntaxhighlight> | ||
To download the source as specified by the package recipe: <syntaxhighlight lang=" | To download the source as specified by the package recipe: <syntaxhighlight lang="console">$ nix-build '<nixpkgs>' -A emacs.src</syntaxhighlight> | ||
The patched source is usually not a derivation itself, but can be produced for most packages with the following command: <syntaxhighlight lang=" | The patched source is usually not a derivation itself, but can be produced for most packages with the following command: <syntaxhighlight lang="console">$ nix-shell '<nixpkgs>' -A emacs \ | ||
--command 'unpackPhase; cd $sourceRoot; patchPhase'</syntaxhighlight> | --command 'unpackPhase; cd $sourceRoot; patchPhase'</syntaxhighlight> | ||
|- | |- | ||
| Line 273: | Line 269: | ||
| | | | ||
| | | | ||
|<syntaxhighlight lang="console">git clone foobar | |<syntaxhighlight lang="console">$ git clone foobar | ||
cat >default.nix <<EOF | $ cat > default.nix <<EOF | ||
with import <nixpkgs> { }; | with import <nixpkgs> { }; | ||
lib.overrideDerivation foobar (oldAttrs : { | lib.overrideDerivation foobar (oldAttrs : { | ||
| Line 280: | Line 276: | ||
}) | }) | ||
EOF | EOF | ||
nix-build</syntaxhighlight> | $ nix-build</syntaxhighlight> | ||
|- | |- | ||
|Install a binary package | |Install a binary package | ||
| | | | ||
| | | | ||
|e.g. via [https://github.com/ | |e.g. via [https://github.com/nix-community/nix-ld#nix-ld nix-ld] | ||
|- | |- | ||
|Install a .deb | |Install a .deb | ||
|<syntaxhighlight lang="console">$ sudo dpkg -i package.deb</syntaxhighlight> | |<syntaxhighlight lang="console">$ sudo dpkg -i package.deb</syntaxhighlight> | ||
| | | | ||
|Install dpkg with Nix, then <syntaxhighlight lang=" | |Install dpkg with Nix, then <syntaxhighlight lang="console">$ dpkg -i package.deb</syntaxhighlight> While this is technically possible it will in all likelihood not work. | ||
|} | |} | ||
</div> | |||
== See also == | |||
* [[Cheatsheet]] | |||
* https://nixcademy.com/cheatsheet/ | |||
** https://nixcademy.com/downloads/cheatsheet.pdf | |||
[[Category:Cookbook]] | |||