Cheatsheet: Difference between revisions

imported>Makefu
No edit summary
imported>Fadenb
Syntax highlighting
Line 54: Line 54:
|-
|-
|Install a package
|Install a package
|<pre>sudo apt-get install emacs</pre>
|<syntaxhighlight lang="nix">sudo apt-get install emacs</syntaxhighlight>
|In /etc/nixos/configuration.nix:
|In /etc/nixos/configuration.nix:
If it's a program add to systemPackages:
If it's a program add to systemPackages:
<pre>
<syntaxhighlight lang="nix">
systemPackages = with pkgs;  
systemPackages = with pkgs;  
                     [ <other packages...> emacs ];</pre>
                     [ <other packages...> emacs ];</syntaxhighlight>
If it's a service add:
If it's a service add:


<pre>services.openssh.enable = true;</pre>
<syntaxhighlight lang="nix">services.openssh.enable = true;</syntaxhighlight>
|<pre>nix-env -i emacs</pre>
|<syntaxhighlight lang="bash">nix-env -i emacs</syntaxhighlight>
Or with collections, add the package to your ~/.nixpkgs/config.nix and run
Or with collections, add the package to your ~/.nixpkgs/config.nix and run
<pre>nix-env -i all</pre>
<syntaxhighlight lang="bash">nix-env -i all</syntaxhighlight>
Since 17.09pre:
Since 17.09pre:
<pre>users.users.<username>.packages =  
<syntaxhighlight lang="nix">users.users.<username>.packages =  
           with pkgs;[ emacs ];</pre>
           with pkgs;[ emacs ];</syntaxhighlight>
|-
|-
|Uninstall a package
|Uninstall a package
|<pre>sudo apt-get remove emacs</pre>
|<syntaxhighlight lang="bash">sudo apt-get remove emacs</syntaxhighlight>
|remove from /etc/nixos/configuration.nix
|remove from /etc/nixos/configuration.nix
<pre>sudo nixos-rebuild switch</pre>
<syntaxhighlight lang="bash">sudo nixos-rebuild switch</syntaxhighlight>
|
|
|-
|-
|Uninstall a package removing its configuration
|Uninstall a package removing its configuration
|<pre>apt-get purge emacs</pre>
|<syntaxhighlight lang="bash">apt-get purge emacs</syntaxhighlight>
|All configuration is in configuration.nix
|All configuration is in configuration.nix
|
|
|-
|-
|Update the list of packages
|Update the list of packages
|<pre>sudo apt-get update</pre>
|<syntaxhighlight lang="bash">sudo apt-get update</syntaxhighlight>
|<pre>sudo nix-channel --update</pre>
|<syntaxhighlight lang="bash">sudo nix-channel --update</syntaxhighlight>
|<pre>nix-channel --update</pre>
|<syntaxhighlight lang="bash">nix-channel --update</syntaxhighlight>
|-
|-
|Upgrade packages
|Upgrade packages
|<pre>sudo apt-get upgrade</pre>
|<syntaxhighlight lang="bash">sudo apt-get upgrade</syntaxhighlight>
|<pre>sudo nixos-rebuild switch</pre>
|<syntaxhighlight lang="bash">sudo nixos-rebuild switch</syntaxhighlight>
|<pre>nix-env -u</pre>
|<syntaxhighlight lang="bash">nix-env -u</syntaxhighlight>
|-
|-
|Check for broken dependencies
|Check for broken dependencies
|<pre>sudo apt-get check</pre>
|<syntaxhighlight lang="bash">sudo apt-get check</syntaxhighlight>
|<pre>nix-store --verify --check-contents</pre>
|<syntaxhighlight lang="bash">nix-store --verify --check-contents</syntaxhighlight>
|unneeded!
|unneeded!
|-
|-
|List package dependencies
|List package dependencies
|<pre>apt-cache depends emacs</pre>
|<syntaxhighlight lang="bash">apt-cache depends emacs</syntaxhighlight>
|<pre>nix-store --query --requisites $(readlink -f /run/current-system)
|<syntaxhighlight lang="bash">nix-store --query --requisites $(readlink -f /run/current-system)
nix-store -q --tree /nix/var/nix/profiles/system</pre>
nix-store -q --tree /nix/var/nix/profiles/system</syntaxhighlight>
|<pre>nix-store --query --references\
|<syntaxhighlight lang="bash">nix-store --query --references\
   $(nix-instantiate '<nixpkgs>' -A emacs)</pre>
   $(nix-instantiate '<nixpkgs>' -A emacs)</syntaxhighlight>
For installed packages:
For installed packages:
<pre>nix-store --query --references $(which emacs)</pre>
<syntaxhighlight lang="bash">nix-store --query --references $(which emacs)</syntaxhighlight>
|-
|-
|List which packages depend on this one (reverse dependencies)
|List which packages depend on this one (reverse dependencies)
|<pre>apt-cache rdepends emacs</pre>
|<syntaxhighlight lang="bash">apt-cache rdepends emacs</syntaxhighlight>
|
|
|For installed packages (only print reverse dependencies *which are already installed*):
|For installed packages (only print reverse dependencies *which are already installed*):
<pre>nix-store --query --referrers $(which emacs)</pre>
<syntaxhighlight lang="bash">nix-store --query --referrers $(which emacs)</syntaxhighlight>
|-
|-
|Verify all installed packages
|Verify all installed packages
|<pre>debsums</pre>
|<syntaxhighlight lang="bash">debsums</syntaxhighlight>
|<pre>sudo nix-store --verify --check-contents</pre>
|<syntaxhighlight lang="bash">sudo nix-store --verify --check-contents</syntaxhighlight>
|<pre>nix-store --verify --check-contents</pre>
|<syntaxhighlight lang="bash">nix-store --verify --check-contents</syntaxhighlight>
|-
|-
|Fix packages with failed checksums
|Fix packages with failed checksums
|Reinstall broken packages
|Reinstall broken packages
|<pre>sudo nix-store --verify --check-contents --repair</pre>
|<syntaxhighlight lang="nix">sudo nix-store --verify --check-contents --repair</syntaxhighlight>
|<pre>nix-store --verify --check-contents --repair</pre>
|<syntaxhighlight lang="nix">nix-store --verify --check-contents --repair</syntaxhighlight>
|-
|-
|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.
|<pre>nix-channel --add\
|<syntaxhighlight lang="bash">nix-channel --add\
   https://nixos.org/channels/nixpkgs-unstable <name></pre>
   https://nixos.org/channels/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.
<pre>nix-channel --remove <name></pre>
<syntaxhighlight lang="bash">nix-channel --remove <name></syntaxhighlight>
To eliminate a channel.
To eliminate a channel.
<pre>nix-channel --list</pre>
<syntaxhighlight lang="bash">nix-channel --list</syntaxhighlight>
To show all installed channel.
To show all installed channel.
|When run by a user channels work locally, when run by root they're used as the system-wide channels.
|When run by a user channels work locally, when run by root they're used as the system-wide channels.
Line 145: Line 145:
|-
|-
|Configure a package
|Configure a package
|<pre>sudo dpkg-reconfigure <package></pre>
|<syntaxhighlight lang="bash">sudo dpkg-reconfigure <package></syntaxhighlight>
|edit /etc/nixos/configuration.nix
|edit /etc/nixos/configuration.nix
|edit ~/.nixpkgs/config.nix TODO More details about how to edit
|edit ~/.nixpkgs/config.nix TODO More details about how to edit
Line 162: Line 162:
|-
|-
|Find packages
|Find packages
|<pre>apt-cache search emacs</pre>
|<syntaxhighlight lang="bash">apt-cache search emacs</syntaxhighlight>
|<pre>nix-env -qaP '.*emacs.*'</pre>
|<syntaxhighlight lang="bash">nix-env -qaP '.*emacs.*'</syntaxhighlight>
|<pre>nix-env -qaP '.*emacs.*'</pre>
|<syntaxhighlight lang="bash">nix-env -qaP '.*emacs.*'</syntaxhighlight>
|-
|-
|Show package description
|Show package description
|<pre>apt-cache show emacs</pre>
|<syntaxhighlight lang="bash">apt-cache show emacs</syntaxhighlight>
|<pre>nix-env -qa --description '.*emacs.*'</pre>
|<syntaxhighlight lang="bash">nix-env -qa --description '.*emacs.*'</syntaxhighlight>
|<pre>nix-env -qa --description '.*emacs.*'</pre>
|<syntaxhighlight lang="bash">nix-env -qa --description '.*emacs.*'</syntaxhighlight>
|-
|-
|Show files installed by package
|Show files installed by package
|<pre>dpkg -L emacs</pre>
|<syntaxhighlight lang="bash">dpkg -L emacs</syntaxhighlight>
|<pre>readlink -f $(which emacs)
|<syntaxhighlight lang="bash">readlink -f $(which emacs)
  /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4/bin/emacs-24.4</pre>
  /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4/bin/emacs-24.4</syntaxhighlight>
then
then
<pre>du -a /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4</pre>
<syntaxhighlight lang="bash">du -a /nix/store/ji06y4haijly0i0knmr986l2dajffv1p-emacs-24.4</syntaxhighlight>
|<pre></pre>
|<syntaxhighlight lang="bash"></syntaxhighlight>
|-
|-
|Show package for file
|Show package for file
|<pre>dpkg -S /usr/bin/emacs</pre>
|<syntaxhighlight lang="bash">dpkg -S /usr/bin/emacs</syntaxhighlight>
|follow the symlink
|follow the symlink
|follow the symlink
|follow the symlink
Line 187: Line 187:
|-
|-
|Start a service
|Start a service
|<pre>sudo service apache start</pre>
|<syntaxhighlight lang="bash">sudo service apache start</syntaxhighlight>
|<pre>sudo systemctl start apache</pre>
|<syntaxhighlight lang="bash">sudo systemctl start apache</syntaxhighlight>
|
|
|-
|-
|Stop a service
|Stop a service
|<pre>sudo service apache stop</pre>
|<syntaxhighlight lang="bash">sudo service apache stop</syntaxhighlight>
|<pre>sudo systemctl stop apache</pre>
|<syntaxhighlight lang="bash">sudo systemctl stop apache</syntaxhighlight>
|
|
|-
|-
Line 203: Line 203:
|Adding a user
|Adding a user
|sudo adduser alice
|sudo adduser alice
|Add <pre>users.extraUsers.alice =
|Add <syntaxhighlight lang="nix">users.extraUsers.alice =
  { isNormalUser = true;
  { isNormalUser = true;
   home = "/home/alice";
   home = "/home/alice";
Line 210: Line 210:
   openssh.authorizedKeys.keys =
   openssh.authorizedKeys.keys =
       [ "ssh-dss AAAAB3Nza... alice@foobar" ];
       [ "ssh-dss AAAAB3Nza... alice@foobar" ];
  };</pre> to to /etc/nixos/configuration.nix and then call <pre>nixos-rebuild switch</pre>
  };</syntaxhighlight> to to /etc/nixos/configuration.nix and then call <syntaxhighlight lang="nix">nixos-rebuild switch</syntaxhighlight>
|
|
|-
|-
Line 216: Line 216:
|-
|-
|List binaries
|List binaries
|<pre>ls /usr/bin/</pre>
|<syntaxhighlight lang="bash">ls /usr/bin/</syntaxhighlight>
|<pre>ls /run/current-system/sw/bin &&\
|<syntaxhighlight lang="bash">ls /run/current-system/sw/bin &&\
ls /nix/var/nix/profiles/default/bin/</pre>
ls /nix/var/nix/profiles/default/bin/</syntaxhighlight>
|<pre>ls ~/.nix-profile/bin</pre>
|<syntaxhighlight lang="bash">ls ~/.nix-profile/bin</syntaxhighlight>
|-
|-
|Get the current version number
|Get the current version number
|<pre>cat /etc/debian_version</pre>
|<syntaxhighlight lang="bash">cat /etc/debian_version</syntaxhighlight>
|<pre>nixos-version</pre>
|<syntaxhighlight lang="bash">nixos-version</syntaxhighlight>
|<pre>nixos-version</pre>
|<syntaxhighlight lang="bash">nixos-version</syntaxhighlight>
|-
|-
|Get sources for a package
|Get sources for a package
|<pre>apt-get source emacs</pre>
|<syntaxhighlight lang="bash">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 recipe: <pre>grep -r emacs $(nix-instantiate --eval --expr '<nixpkgs>')</pre>
To find the package recipe: <syntaxhighlight lang="bash">grep -r emacs $(nix-instantiate --eval --expr '<nixpkgs>')</syntaxhighlight>
To download the source as specified by the package recipe: <pre>nix-build '<nixpkgs>' -A emacs.src</pre>
To download the source as specified by the package recipe: <syntaxhighlight lang="bash">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: <pre>nix-shell '<nixpkgs>' -A emacs\
The patched source is usually not a derivation itself, but can be produced for most packages with the following command: <syntaxhighlight lang="bash">nix-shell '<nixpkgs>' -A emacs\
   --command 'unpackPhase; patchPhase'</pre>
   --command 'unpackPhase; patchPhase'</syntaxhighlight>
|-
|-
|Compile & install a package from source
|Compile & install a package from source
|
|
|
|
|<pre>git clone foobar
|<syntaxhighlight lang="bash">git clone foobar
cat >default.nix <<EOF
cat >default.nix <<EOF
with import <nixpkgs> { };
with import <nixpkgs> { };
Line 246: Line 246:
})
})
EOF
EOF
nix-build</pre>
nix-build</syntaxhighlight>
|-
|-
|Install a binary package
|Install a binary package
Line 254: Line 254:
|-
|-
|Install a .deb
|Install a .deb
|<pre>dpkg -i package.deb</pre>
|<syntaxhighlight lang="bash">dpkg -i package.deb</syntaxhighlight>
|
|
|Install dpkg with Nix, then <pre>dpkg -i package.deb</pre>
|Install dpkg with Nix, then <pre>dpkg -i package.deb</syntaxhighlight>
|}
|}