Cheatsheet: Difference between revisions

imported>Mic92
No edit summary
imported>Mic92
No edit summary
Line 260: Line 260:
== Get the store path for a package ==
== Get the store path for a package ==


<source lang="javascript">
<source lang="nix">
$ nix-repl
$ nix-repl
nix-repl> :l <nixpkgs>
nix-repl> :l <nixpkgs>
Line 269: Line 269:
$ nix-build '<nixpkgs>' --no-build-output -A xorg.libXtst
$ nix-build '<nixpkgs>' --no-build-output -A xorg.libXtst
/nix/store/nlpnx21yjdjx2ii7ln4kcmbm0x1vy7w9-libXtst-1.2.3
/nix/store/nlpnx21yjdjx2ii7ln4kcmbm0x1vy7w9-libXtst-1.2.3
</source>
### Adding files to the store
It is sometimes necessary to add files to the store manually.
This is particularly the case with packages that cannot be downloaded automatically,
for example, proprietary software packages.
For most files, it is sufficient to run:
<source lang="bash">
$ nix-store --add-fixed sha256 /path/to/file
</source>
Unfortunately, `nix-store` will try to load the entire file into memory,
which will fail if the file size exceeds available memory.
If we have root access, we can copy the file to the store ourselves:
<source lang="bash">
$ sudo unshare -m bash  # open a shell as root in a private mount namespace
$ largefile=/path/to/file
$ hash=$(nix-hash --type sha256 --flat --base32 $largefile)  # sha256 hash of the file
$ storepath=$(nix-store --print-fixed-path sha256 $hash $(basename $largefile))  # destination path in the store
$ mount -o remount,rw /nix/store  # remount the store in read/write mode (only for this session)
$ cp $largefile $storepath  # copy the file
$ printf "$storepath\n\n0\n" | nix-store --register-validity --reregister  # register the file in the Nix database
$ exit  # exit to the original shell where /nix/store is still mounted read-only
</source>
== Build nixos from nixpkgs repo ==
The following snippet will build the system from a git checkout:
<source lang="bash">
$ nixos-rebuild -I nixpkgs=/path/to/nixpkgs switch
</source>
This method can be used when testing nixos services for a pull request to nixpkgs.
Building nixos from a git is an alternative to using nix channels and set up permanent following this [blog article](http://anderspapitto.com/posts/2015-11-01-nixos-with-local-nixpkgs-checkout.html).
It has a couple of advantages over nixpkgs as it allows back-porting of packages/changes to stable versions
as well as applying customization.
Use the following command to build directly from a particular branch of a repo in github:
<source lang="bash">
$ nixos-rebuild -I nixpkgs=https://github.com/nixcloud/nixpkgs/archive/release-17.03.tar.gz switch
</source>
## Building a service as a VM (for testing)
While `nixos-rebuild build-vm` allows to build a vm out of the current system configuration, there is a more light-weight alternative when only a single service needs to be tested.
Given the following configuration:
<source lang="nix">
# vm.nix
{ lib, config, ... }:                                                                                                 
{                                                                                                                                     
  services.tor.enable = true;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
  users.users.root.initialPassword = "root";                                                                                         
}
</source>
a vm can be build using the following command:
<source lang="bash">
$ nixos-rebuild -I nixpkgs=/path/to/nixpkgs -I nixos-config=./vm.nix build-vm
</source>
where `-I nixpkgs=/path/to/nixpkgs` is optionally depending whether the vm should be build from git checkout or a channel.
On non-nixos (linux) systems the following command can be used instead:
<source lang="bash">
nix-build '<nixpkgs/nixos>' -A vm -k -I nixos-config=./vm.nix
</source>
== Reuse a package as a build environment ==
As packages already contains all build dependencies, they can be reused to a build environment quickly.
In the following a setup for the cmake-based project [bcc](https://github.com/iovisor/bcc) is shown.
After obtaining the source:
<source lang="bash">
$ git clone https://github.com/iovisor/bcc.git
$ cd bcc
</source>
Add the following `default.nix` to the project:
<source lang="nix">
with import <nixpkgs> {};
linuxPackages.bcc.overrideDerivation (old: {
  # overrideDerivation allows it to specify additional dependencies
  buildInputs = [ bashInteractive ninja ] ++ old.buildInputs;
})
</source>
To initiate the build environment run `nix-shell` in the project root directory
<source lang="bash">
# this will download add development dependencies and set up the environment so build tools will find them.
$ nix-shell
</source>
The following is specific to bcc or cmake in general:
(so you need to adapt the workflow depending on the project, you hack on)
<source lang="bash">
$ mkdir build
$ cd build
# cmakeFlags is also defined in the bcc package. autotools based projects might defined $configureFlags
$ eval cmake $cmakeFlags ..
$ make
</source>
== Customizing Packages ==
=== Upgrading individual packages to a different channel ===
One can track multiple channels on NixOS simultaneously, and then declaratively change packages from the default channel to another one.
For example one can have both the unstable and stable channels on system root:
<source lang="nix">
$ sudo nix-channel --list
nixos https://nixos.org/channels/nixos-17.03
nixos-unstable https://nixos.org/channels/nixos-unstable
</source>
and the following in `configuration.nix`:
<source lang="nix">
nixpkgs.config = {
  # Allow proprietary packages
  allowUnfree = true;
  # Create an alias for the unstable channel
  packageOverrides = pkgs: {
    unstable = import <nixos-unstable> {
      # pass the nixpkgs config to the unstable alias
      # to ensure `allowUnfree = true;` is propagated:
      config = config.nixpkgs.config;
    };
  };
};
</source>
which allows you to switch particular packages to the unstable channel:
<source lang="nix">
environment = {
  systemPackages = with pkgs; [
    ddate
    devilspie2
    evince
    unstable.google-chrome
    # ...
    zsh
  ];
};
</source>
== Building statically linked packages ==
<source lang="bash">
$ nix-build -E 'with (import ./. {}); (curl.override { stdenv = makeStaticLibraries stdenv;}).out'
</source>
== Rebuild a package with debug symbols ==
<source lang="bash">
$ nix-build -E 'with import <nixpkgs> {}; enableDebugging st'
$ file result/bin/st
result/bin/st: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/f111ij1fc83965m48bf2zqgiaq88fqv5-glibc-2.25/lib/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, not stripped, with debug_info
</source>
</source>