Skip to content

nixcloud/nixpkgs-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

nix related workshops

welcome to the second workshop on the cccamp2015, after we've been using nix-shell to instantiate custom environments with toolchains for developing software we are now going into nixpkgs and how software is packaged:

so, let's get started...

nix programming / language introduction

a good start into nix/nixos is reading the wiki at [1] as it provides links to all important documentations.

question: find out what the difference between nix/nixos/nixpkgs documentation coverage is.

nixpkgs construction

all packages and services are packaged inside nixpkgs, so we need a copy of that. You need to check them out from github:

  1. go to https://github.com/nixos/nixpkgs and get the url required to clone this repository.

  2. switch to your local machine and clone the repository with:

     git clone 'url you just looked up'
    

    The problem now is that we have the newest version of the NIXPKGS. This means we are ahead of the nixchannel. This can result in a rebuild of many packages. To avoid this we are going to get the same NIXPKGS version as it is used on our local machine. Doing so enables us to recieve packages from the binary cache.

  3. move into the nixpkgs directory:

     cd nixpkgs
    
  4. get your local nixversion:

     nixos-version
    

    Your result should loock similar to this:

     15.07pre66213.9d5508d (Dingo)
    
  5. Now we can checkout the same version from git using the last part of the version number. (In the example it is 9d5508d, you should replace it with your nixos-version number).

     git checkout 9d5508d
     git checkout -b 'fix/pkg-name-update'
    

the directory listing ls -la now lists several directories:

  • lib a bunch of library functions, see lib/attrsets.nix, there are lots of examples included on how to use the functions provided for attribute sets, in short; attrsets

  • pkgs contains all the software (or packages) in form of nix-expressions called default.nix. see pkgs/top-level and especially pkgs/top-level/all-packages.nix and

  • nixos contains nixos/modules/ which represents all services which can be configured on nixos, see http://nixos.org/nixos/options.html for possible configuration values. nixos/tests also provides unit tests which will be covered in the https://nixos.org/wiki/NixOS_Chaos_Communication_Camp_2015#workshop:_nixos_unit_tests workshop.

question: nix-env -I nixpkgs=/your/directory/nixpkgs -qaP uses your local checkout instead of the one provided by nix-channel. edit `nixpkgs/top-level/all-packages.nix and find this line:

attic = callPackage ../tools/backup/attic { };

and change it to

attic-foo = callPackage ../tools/backup/attic { };
ksadfjksadf # this is intentional! (will be removed after we know it is working)

afterwards: nix-env -I nixpkgs=$NIXPKGS -f default.nix -qaP | grep attic

and you should get an error like this:

error: syntax error, unexpected ID, expecting '.' or '=', at /home/joachim/Desktop/projects/nixos/nixpkgs/pkgs/top-level/all-packages.nix:542:3
(use ‘--show-trace’ to show detailed location information)

this error is good as it shows that we are actually using our own checkout of nixpkgs, so remove:

ksadfjksadf # this is intentional! (will be removed after we know it is working)

and issue the command again:

`nix-env -I nixpkgs=$NIXPKGS -f default.nix -qaP | grep attic`

question: the attribute path changed but not the package name. also change the package name now by editing the default.nix in the respective directory and verify the change with running: nix-env -I nixpkgs=$NIXPKGS -f default.nix -qaP | grep attic again.

finally install the software:

`nix-env -I nixpkgs=$NIXPKGS -f default.nix -i attic-foo

question:

  • discuss: why is there a package name "attic-foo" and attribute "attic-foo-0.14"? (please see the packages and options) section below, where this is explained further!
  • how is the resulting binary called, after you installed it? is it called attic or attic-foo?
  • looking at the default.nix, what are the dependencies of the package?

packages and options

nix-env -qaP shows a list of packags, read http://nixos.org/nix/manual/#sec-expression-syntax and look for two important things: attribute and name.

question: what is the difference between an attribute and a name. issue this command: nix-env -qaP | grep qt4 and use nix-shell to get a custom environment with nixos.pkgs.qt5Full inside.

note: if you are in your qt5Full environment issue qmake --version to verify that it is working.

now go to your nixpkgs checkout

http://nixos.org/nixpkgs/manual/#chap-functions

example

We want to extend the nano pkg with syntax highliting for '.nix' files. This requires to include the 'nix.nanorc' file into the installation path of nixos.

  1. Navigate into your nixpkgs directory and find the directory which holds all nano related files. At the moment there is only the 'default.nix'. Add the 'nix.nanorc' from this repository to the nano directory.

  2. Now we need to adjust the 'default.nix' in a way that adds the 'nix.nanorc' to the installation folder. Open the 'default.nix' with an editor of your choise. We now add a 'hook' that enables us to acces the 'nix.nanorc' after the installation. Add the following code to your 'default.nix'.

     hook = ./nix.nanorc;
    
  3. Add another attribute with the name 'postInstall'. Within this Attribute we define what should happen after nano is installed. In our case we want to copy the 'nix.nanorc' into the 'share/nano/' directory within the installation folder.

    Write a small script (within the default.nix) that copies the nix.nanorc into the 'share/nano/' directory. You can embed some nixvariables into the string. For example $out is the installation path of nano and ${hook} is the nix.nanorc file.

     postInstall = '' TODO '';
    
  4. install nano

  5. Enjoy the new syntaxhighliting. (If you are working on your own machine you have to enable syntaxhighliting first. You can do this by adding programs.nano.nanorc ="include ${pkgs.nano}/share/nano/*.nanorc" to your configuration.nix)

understanding how dependencies in nixos work

deploying your custom software using nix-env/nixos-rebuild/nix-shell

documentation