From 550748da7717ca82d33fc62eabda818139babcd2 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Wed, 16 Oct 2024 01:53:45 +0100 Subject: [PATCH] assignment: added section on the nix programming language --- _git.tar.gz | 4 ++-- src/skynet/nix.md | 59 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/_git.tar.gz b/_git.tar.gz index bfb994e..27b759b 100644 --- a/_git.tar.gz +++ b/_git.tar.gz @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:26358636ab2431f4688f5a3a8e2c47fd59ad842d840488b4d9577791d192ba64 -size 341053 +oid sha256:d68d0b80a4c4150a73887f196b992c2ce1a75dcddae7d6b80705d241cd5e69d0 +size 344824 diff --git a/src/skynet/nix.md b/src/skynet/nix.md index f8d2305..094cdd9 100644 --- a/src/skynet/nix.md +++ b/src/skynet/nix.md @@ -17,14 +17,67 @@ 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 eitehr be used as the input of another program or be used as is by the system/user. +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) #### 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 @@ -54,4 +107,6 @@ An example using different versions of Firefox: [nixos_skynet]: https://forgejo.skynet.ie/Skynet/nixos -[nix_paper]: https://edolstra.github.io/pubs/nspfssd-lisa2004-final.pdf \ No newline at end of file +[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/# \ No newline at end of file