# Nix/NixOS The [Skynet Cluster][nixos_skynet] is (for the most part) running on a Linux variant called NixOS. This article aims to introduce you to Nix and Nixos in order to get you up to speed to administer the cluster. ## What it is ### Nix #### Package Manager The word Nix refers to two things: a language and a package manager. These are deeply interlinked together with the language being how the package manager is able to do its job. Nix grew out of a [PhD by Eelco Dolstra][nix_paper] wherein he proposes a slightly different way to manage dependencies on a system. For most Linux systems programs make use of other software installed on the computer, for the most part this works fine. Where issue may arise is if one program needs to update one of these dependencies, specially a minor or major patch where backwards compatibility is not guaranteed. If another program is using this (system wide) dependency then it may run into interface issues when using it. In a sense updating one program can break another on the system. The route the Nix package manager takes is it treats each program as a function. Using teh Nix language a function for that package is created which states what inputs are required, what is needed to turn those inputs into teh program as well as the name for the output. The output is then saved in a read only location in the format of ``/nix/store/$hash-program-name-version``. This output can either be used as the input of another program or be used as is by the system/user. Using this format means that any change in the inputs or the program itself will result in a different output. This means that multiple versions of the program (some even the same version but different commit) can co-exist on the one system. An example using different versions of Firefox: ![img.png](nix/firefox_co-existing.png) ##### Example An example of packaging an application can be found here: [Sieve Editor GUI on Nixpkgs][nix_pkgs_sieve] This is packaging up a GUI node.js application. The application itself allows the user to edit sieve scripts. Once you have [downloaded and installed](#download) Nix you will be able to install and run it like so: ```shell nix-shell -p sieve-editor-gui sieve-editor-gui . ``` #### Language There are two partially difficult problems in computer science: 1. Off by one errors 2. Caching 3. Naming things Nix falls into this last pitfall. The programming language used by teh Nix package manager is called Nix, not Nixlang (as like Erlang) but rather the same name as primary tool that uses it. For clarity for teh remainder of this subsection we are only talking about Nix the language. Nix is a lazily evaluated functional language which al has REPL (Read, Evaluate, Print, and Loop) capability like what you would see in Python. As a whole it takes strong influences from OCaml and other ML derived languages. ##### Types It has most of the normal types that you would expect of a programming language, along with a few extra to deal with the filesystem: ```nix a = 1 # int b = 1.001 # float c = /path/to/thing # path d = "42" # string e = true # boolean ``` Of these the ``path`` type will be new to most people. This can take either an absolute or relative path. ##### Functions If you look at the section below it will seem that these are another type of assignment to a variable. That is half right, these are akin to function pointers that you would see in C or C++. Functions in Nix do not have types for either parameters or return. This is due to it being lazily evaluated, like Python or Javascript. As such the ``double`` function will accept any numeric value ```nix double = x: x*2 mul = a: b: a*b double 2 double 4.2 mul 7 6 ``` ##### Attribute Sets In most languages the way to group data would be either an Object or a Struct. Nix has a similar datastructure: ```nix s = { foo = "bar"; biz = "baz"; } s.foo # bar s.biz # baz ``` ##### More data This is a rough quickstart introduction to Nix. For more detailed information I recommend these resources. * [Official Guide][nix_guide_official] * [Nix Pills][nix_guide_pills] ### Flakes A Flake is one of teh best ways of interacting with nix. Despite it having some issues and still being marked as experimental it has become a de-facto standard. This is also the format that we use in Skynet. The [Official Wiki Page][nix_flake] will be more informative than what can be shoved into this article. ### Nixos ### Lix ## Why we use it {Details of how the config was ascattered and hard to find} {Also embracing devops and reduced manpower} ## How we use it ### Requirements {add warnign that git and git-lfs should also need to be in teh path} ### Download ### Colmena #### Local ##### Building ##### Repl #### Deployment [nixos_skynet]: https://forgejo.skynet.ie/Skynet/nixos [nix_paper]: https://edolstra.github.io/pubs/nspfssd-lisa2004-final.pdf [nix_guide_official]: https://nix.dev/tutorials/first-steps/ [nix_guide_pills]: https://nixos.org/guides/nix-pills/# [nix_pkgs_sieve]: https://github.com/NixOS/nixpkgs/blob/a3c0b3b21515f74fd2665903d4ce6bc4dc81c77c/pkgs/by-name/si/sieve-editor-gui/package.nix [nix_flake]: https://wiki.nixos.org/wiki/Flakes