assignment: added section on the nix programming language

This commit is contained in:
silver 2024-10-16 01:53:45 +01:00
parent d98c090fb7
commit 550748da77
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
2 changed files with 59 additions and 4 deletions

BIN
_git.tar.gz (Stored with Git LFS)

Binary file not shown.

View file

@ -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
[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/#