Reading the Nix expressions language
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 19 | |
Author | ||
License | CC Attribution 3.0 Unported: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/50695 (DOI) | |
Publisher | ||
Release Date | ||
Language |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
|
Nixcon 201914 / 19
10
11
16
17
00:00
Right angleComputer fileType theoryMultiplicationExpressionString (computer science)BitFunctional (mathematics)Performance appraisalPositional notationTraffic reportingData typeAttribute grammarMereologyForcing (mathematics)Mixed realityTheory of relativityFormal languageMultiplication signConstructor (object-oriented programming)Representation (politics)Roundness (object)Poisson-KlammerDefault (computer science)Directory serviceParameter (computer programming)Object (grammar)WordIntegrated development environmentCodeKeyboard shortcutData conversionCASE <Informatik>Pattern languageTerm (mathematics)Exception handlingTrailSpacetimeKey (cryptography)NumberLevel (video gaming)Protein foldingCodeElectronic mailing listDifferent (Kate Ryan album)Interpreter (computing)Subject indexingBus (computing)ResultantMusical ensembleFactory (trading post)Web pageMoment of inertiaAtomic numberAdditionVideo gameRevision controlMathematicsPhysical systemTranslation (relic)Complete metric spaceFlow separationExpected valueProduct (business)Lecture/Conference
09:30
Lambda calculusPerformance appraisalGastropod shellPhysical systemSystem programmingComputer virusString (computer science)InterpolationAttribute grammarKey (cryptography)Error messageSet (mathematics)FeedbackVideo gameKeyboard shortcutCodeMereologySoftware testingDerivation (linguistics)Variable (mathematics)InterpolationExpressionFormal languageOperator (mathematics)Revision controlFile systemPoisson-KlammerNumberComputer fileHierarchyCycle (graph theory)Forcing (mathematics)Different (Kate Ryan album)Slide ruleObject (grammar)Goodness of fitPhase transitionLevel (video gaming)Boolean algebraMixed realityMultiplication signString (computer science)BitSocial classData conversionEvoluteCASE <Informatik>Configuration spaceClassical physicsMoment (mathematics)CalculationoutputInheritance (object-oriented programming)Message passingRadio-frequency identificationRiflingElectric dipole momentExterior algebraClique-widthPhysical systemExpected valueLinear regressionBranch (computer science)WordMusical ensembleLecture/Conference
18:59
PlastikkarteConcentricReading (process)Row (database)WordDisk read-and-write headFunctional (mathematics)Sound effectRight angleMultiplication signMathematicsMusical ensembleCodeFormal languageTouch typingAttribute grammarState observerResultantDerivation (linguistics)Exception handlingOperator (mathematics)Functional programmingKey (cryptography)Sign (mathematics)Goodness of fitUniform resource locatorSheaf (mathematics)Mixed realityRoutingRevision controlMereologyObject (grammar)Server (computing)Fitness functionOrder (biology)Type theoryDefault (computer science)Configuration spaceChainMedical imagingDifferent (Kate Ryan album)Theory of relativityOrbitShooting methodParameter (computer programming)YouTubeParsingQuicksortBuildingCausalityLinear regressionNeuroinformatikBitVotingCovering spaceContent (media)Computer fileOpen sourceSpeech synthesisKeyboard shortcutHash functionLine (geometry)Performance appraisal1 (number)Gastropod shellForm (programming)CurveControl flowBootingNetwork topologyInheritance (object-oriented programming)Scripting languageFile systemProcedural programmingRecursionError messageNamespaceData structureSet (mathematics)Similarity (geometry)Variable (mathematics)Service (economics)Computer hardwareLevel (video gaming)MultilaterationPositional notationMeta elementData managementClique-widthLibrary (computing)Standard deviationTerm (mathematics)Structural loadMiniDiscCategory of beingElectric generatorIndependence (probability theory)Electronic mailing listComputer animationLecture/Conference
Transcript: English(auto-generated)
00:00
We didn't really advertise this track heavily so I am surprised that there are so many people and I am also delighted Maybe we should start with just a quick... So this is more of an interactive session, the goal is for you to learn how to use Inks and kind of go over all of the aspects because it can be a bit difficult, right?
00:24
So maybe we can start with a quick round of... Maybe who knows how to read the next code? Raise your hand Ok, so half of the room Who knows how to create a new package in the next packages?
00:45
A bit less? So today we are going to cover these things So the first talk is how to read next codes So it doesn't look too weird anymore And then we are going to cover how next works in the internals
01:01
How to build your own packages And if there is something that is unclear you should interrupt us And then we can just go over it right Ok, let's get started So, rename the next expression language
01:23
So next is an ecosystem and that's just a small part of it which is the language mix And then we are going to see other parts of the ecosystem So a long time ago, like 3 years which is 30 years in completed time
01:44
I was starting this and I was seeing things like this Or like this And like this And just what I did is I banged my head against and I tried until it works
02:01
And so the goal of this talk is that you don't have to do this And maybe after the end of this talk you will be able to just read the code and it makes sense Ok, so what is the next language? Next language is basically you have JSON like values So it translates actually quite well to JSON data types
02:25
So for example the string is the same representation, number is the same The list is already a bit different because we don't have commas between items And to create an object like you have key equal value
02:43
And then you need a semicolon to end the expression null is null and true force is true So that's the JSON things And if we put them together it looks a bit like this
03:04
Ok, so strings have more constructors or literate representation You can also have multiline strings And so one of the things that you need to know about multiline strings is
03:21
Nix is going to remove this part of the string for you So you can actually align your literate in your code And Nix is going to do the right thing and remove everything that's in front If you push this a bit further then it's not going to remove this
03:42
It's just going to remove the smallest part of the right spaces And then finally we have a literate like representation So you can just type yours But this part is going to We're thinking of debrigading it but I'm just mentioning it here
04:00
Ok, so that's strings There is another type that's called the path And that's not in the JSON data types And that allows you to reference files correctly So there's multiple notations There's one that's just you type a relative path
04:23
And that's going to be reserved to an absolute path Relative to the file where it's being loaded So it's easy to think about it Then you can use absolute path And it also completes with the tid based on your home directory And then the last one
04:40
Which you probably are going to only see with Nix packages Is this where the bracket notation This is actually a look into the Nix path environment So I'm just mentioning it for now You just need to know that this is going to be reserved to a path
05:01
And if you want to know what path it's going to be You have to look at the Nix path environment Bible And then there is a key equal value So you can see Nix packages on the right here So I have my own Nix packages check out So I just wanted my hit report
05:24
Alright so now we have only values for now So now we enter these functions So Nix functions are just methods So here for example you have one function
05:41
Each function has one argument And then it returns another function with one argument And then that's the body of the function So here we bind the value to a We bind the value to b And in terms of scoping This creates a new scope The scope is only below the function
06:04
So you don't pollute You can never go like You cannot do like in JavaScript where you declare a Bible It's not going to go globally right And then there is another representation which is a keyword argument So again that's one function
06:22
With one argument But this is going to be a bit like pattern matched You pass an attribute set like an object And it's going to extract the keys from the object And bind them into a new scope So that's two ways of writing it
06:43
The most popular one is this one And we're going to see more examples after So here that's the simple way But then if you pass for example an object that has a, b and c Here it's going to fail the evaluation
07:01
Because it's going to complain that c is not the word in here So there's another You can do more things with this For example you can specify default values You can say I don't care about any extra arguments So that's going to be ignored And you can also bind the original attributes that's been passed to the functions
07:25
Who are also a variable Okay so now we have So now we can build just values A little bit of functions And now we start looking into some of the built-ins functions
07:44
So these are functions provided by the interpreter And they are kind of existing functions in the environment So you have two string imports About two string index
08:01
There's no automatic conversion You have to specify if you want to convert from one type to another Except in one case I won't talk about it You can import a file So what you can do is You can say import another next file And it's going to be evaluated
08:21
And then the value from this evaluation is returned to your current scope You can do exceptions Do this Like functional type things like map and fold And functions like that So these are all top level
08:42
And then we have another attribute which is built-ins And then in there you find more functions So there's a trace that allows you to just print out things for debugging That's kind of convenient From JSON to JSON
09:02
And there's many more but I'm not going to cover all of them right now Okay And keywords So there's a few keywords on top of that And then we're going to finish with the language basically So the let binding allows you to bind values to names
09:24
And so here we actually create a new scope Where we bind hello and packages into the scope And then they are available below So you can access them One thing to note here You might notice that we access packages here
09:44
Because the names are bound at the same time So you can do things like You don't care about the ordering of the let binding basically And because Next is a lazy language If you never access one of these values
10:03
There's never going to be a value to it The rec is a bit of the same So if you have an attribute set And you add the rec keyword What it's going to do is the same Like it's going to bind all of these keys
10:22
As variables actually in the scope So sometimes you see a rec keyword And you're wondering why that is It's just because it binds these values into the scope And it's available also below Here you can see the version Another keyword that imports all of the values
10:53
Of the attributes that you pass into the scope This one is a bit dangerous Like in most languages where you have this keyword
11:03
Like here we do with packages And then magically it's not very explicit Like you have these variables that are there Excuse me What is the difference between with and import?
11:20
Okay, good question I need to start the next problem So import allows you to do things like
11:44
Here you get that's the value of the file And then you can bind it to a variable Whereas the with keyword is going to be with
12:00
And then here you say with a equal to 4 A So you only take some part of the packages Not all of them And bind them Like import allows you to load the code from somewhere else
12:25
The with is just a way of importing the existing attributes Into keys into your scope Only some of them, right? All of them Import, like with is a different construct
12:44
Import is to load a new file into your scope And with is a way to deconstruct an attribute into different variables Does that make sense?
13:06
So here packages for example have been imported somewhere before And now we just create the scope And then these are variables that are available magically Not magically but they are implicit
13:26
There is another tool keyword called inherit And that is a short hand tool Basically instead of writing gcc equal to pkgs.gcc You can say inherit from pkgs.gcc
13:43
And so it is just a way to reduce a bit of repetition And you can also use it that way Especially when you inherit the values So you don't repeat like pkgs and pkgs So if you make a typo
14:01
The main advantage is you avoid making typos If the null is stochastic And then assert This allows you to add another expression just before So you have your code without the assert
14:22
And you can insert the assert just here So that is when you are debugging You want to make some assumptions and verify them You add an assert And you say Because the next error messages are not super clear always So what you do is you sprinkle some assert in your code
14:43
And you are like Okay I think this should be what you can set Then you get a better feedback Alright Yeah we are almost there Now we have operators There is a way of constructing objects that is shorter than with JSON
15:05
So you don't need to You can create a deep hierarchy with just adding the dots in between So if you do file system dot slash dot device For this value it actually builds
15:21
Actually I should show you directly
15:47
So now we have a So now I have a deep object Basically with a dot file systems dot slash
16:03
You can see there is a device in here So you are going to encounter this in the next class configuration
16:20
Okay there is another keyword that is OR Which allows you to test if this attribute exists Then it is going to return this If it does exist then you return this alternative value And finally you have this test operator that allows you to test
16:42
It just returns a true force if the key exists in the attribute test There are a few Boolean operators Like classic string interpolation So that is where conversion is done automatically
17:00
Or in some cases where it converts to string But not always So here for example we access this We get the string And we build this string object from this value
17:20
There is a merge operator for attribute sets It is flat so it doesn't do a deep merge Just good to know Excuse me? On the previous slide you had out I forgot to say
17:43
So when you read the next code in X packages for example You see and that is confusing You see the dollar with this And then without the brackets right? So this one is on the mix language level
18:03
So that is the interpolation And then this is later on used in the build phase Which we are going to cover later This is just a bash variable actually So they look the same but they are actually used at different moments
18:24
Because in X you have the evolution that happens Then you produce derivations And then you build the derivations So you have a whole life cycle We are going to cover it later
18:40
Like number operators and stuff So that is it for the language Do I have a volunteer to read? Based on everything I said You should be able to read this code
19:09
Alright so come over here It is more than ideal
19:22
So the first line is the definition of a function right? So we have two arguments Standard def and then fetch URL Then we have a body of a function Which says that we want to
19:42
I don't understand this part I know that this is the definition of some data But I don't see a sign So what does it mean? So this is an attribute set And we access this value
20:01
Which turns out it is a function And then we pass all of this Ah the whole object is already record We pass the record In the record we have values And attributes and values Also one attribute is generated by a function that was passed
20:23
With the argument fetch URL And this takes also something This is an attribute set So what is the difference between rec and the attribute set? Great so this attribute set is normal This one has this rec word that adds all of these keys to your scope
20:49
So this means that for example Scope of the function or which scope? So if it creates a new scope That inherits from the parent scope So here we have a new scope
21:03
Which contains s, t and then f Now when we enter here We have a new scope that inherits from this Or it still has access from that Plus it adds all these values And this one has no parent scope right?
21:20
It doesn't inherit So it has separate We need a pname over here We need a version over here So it has a scope of the parent It's always recursive the scope Yeah recursive is probably what it means So this one only has a parent And this one has a parent and parent of a parent
21:42
And parent of a parent right? You can refer to the p of that object itself Right? You can even like You can even refer to names within other keys That's what it means It's like recursive It's not a keyword rec as well It's recursive Yeah I guess so
22:02
I still don't understand So this one is using keys from the parent And this one is also using keys from the parent So what is the difference? So when you type rec you create a new scope right? And here we have no new scope It's the same It's always
22:20
The scope is always inherited Okay But then you can extend the scope with new scope I understand I understand So this one is sending only those keys They are using the scope of the Where they are initialized Yes I have a question maybe to clarify If I were to use URL
22:41
The URL key within that attribute set I wouldn't be able Unless I put rec before the attribute set Is that correct? Yes So what you are asking is If I need the URL maybe in another key in here Within that attribute set?
23:01
Then you would have to add the rec Okay So that's what rec does basically Yes Okay On the same way it's recursive Okay Me too Thank you So This code still work if we wouldn't use the recursive The rec on the third line
23:20
If you remove this Then p-name and version are not added to the scope And then we wouldn't be able to So anybody that's here And then anybody here But you can You can also So here we use it below
23:40
But you can also use it over here Okay so the next line is We assign the attribute from true Then we have another attribute That's called meta And it says Generating the Another attribute set But with the
24:01
With lib Attribute Right Come from the lib Yes So we can here inside We can use attribute from lib Yeah Okay so So this one Has access to lib
24:20
Without having to Use the name space right Because with name space we would Also access this right If I use something here S dot lib dot license It would work right Yes Okay so with with We have the short hand notation Yes Okay
24:40
And then This generates the The whole thing for meta Yes Lovely Thank you Yeah thanks So basically the value
25:02
Is what we got by the function Oh yes Is the result of the function So the whole file is A function right And if you import this You get the function Okay And if I call it with an attribute set With those two keys It will return Another attribute set
25:21
Oh no It will return The result of In hooking Just make the revision The revision function Yes Okay That takes another attribute set Yeah Okay cool Thank you So this is how we build things Yes Should we do another one?
25:42
Yeah Anybody else here for this one? It's your turn
26:01
So this is configuration.nix This is nixos stuff Not the nix package manager I'm completely new to nix So I'm just busking this It's okay You just need to read the nix script panel Okay You import config and packages And you The hardware configuration nix
26:21
Is another bit of nix code That you're reporting into this file So this is a function Yeah So yeah basically you're Again you're building a new file with a function Of the dot But this time we ignore extra parameters
26:41
Yeah I have a question So the idea of ignoring those parameters Is to make sure that Whatever we pass as an attribute set Will work As long as it has
27:00
A config and packages Alright And we don't care about the other ones Yeah Okay So sometimes you want to be strict And be explicit about all the arguments Okay Sometimes it's So yeah we have this attribute set And then we
27:21
Assign to this key So yeah so the contents of hardware configuration nix Becomes all the definitions in there Become part of this And that's it yeah All the So now you're going a bit too fast Oh okay
27:41
Right now we're just reading the nix code Okay So we're thinking about what does it produce In terms of nix values And things like that Okay But yeah the end result is going to be That you include this configuration Yes Is it Because And specify there Isn't it just a path
28:01
Yeah so right now Exactly So right now all we do is we Actually assign this array That contains a path That's it like it's just a path for now And we assign this to this attributes We're not just using the ebox function
28:23
To import the file Yes Yes you could But that's more how nixxers is configured What we do under the hood is We're going to import it but a bit later So we have functional config and package
28:42
Where do we use those attributes or variables or whatever Ahh I don't Actually I could remove them actually I go around the edges Okay And how do you So here we have like services.shd
29:02
And boots.load How do we know Where they exist I mean For this code right Yeah for now we don't know On this level All we do is we build this big tree of config right So it's like attributes
29:20
Boot that's a new attribute set And we create the big tree And we assign these values And that's it And then in another talk we're going to talk about How all of this gets together And all the nixxers Yeah what's good Thank you
29:41
I need a question Because One of the things I found quite confusing is how I don't really understand I can still How keywords works in nixxers Because obviously there is like we use import But it's not like the keyboard import Doing the import right
30:00
This is just a variable It's a function actually So yeah So how does it work I'll kind of import this to the front So you can over load it Or overwrite it right Yes Yeah well yeah but I believe you can name it import Right or not
30:21
Or is it a simple keyboard It's like this sort of thing What is the keyboard And what is the function So the keywords are So you have a bunch of Built in functions Which are basically by default You have a scope with all of these functions
30:41
Available Then the keywords are More in the like the parsing Section of the Yeah when you parse the next code It's gonna use these Yeah so import is keyboard Yeah So it's a function It's a function
31:00
Okay but this import is still function So I can I can define my own import My scope will be like That import which I will break everything under Yes Can you go back to this example Yeah So the important thing to see here Is this is not import
31:21
Import is a function but this is imports Yeah yeah yeah Oh right Yeah so this imports Mix doesn't know what imports means And so we're not overloading anything in mix We're just making An attribute set with a list of Yeah yeah yeah but if we Name it to import
31:41
And then assign the array To import it will Stop being function in our scope right It will be just an array of Yeah I guess you can shadow You can shadow You can do shadowing Of existing variables with New variables but only below Yeah yeah new scope
32:00
This is what I found Confusing a bit with the recursion Because it records So like sometimes you have like left Right and you find something and you have like Same attribute and you're like What why is it different Yeah Yeah agreed like the The let's binding And the
32:21
And the width Kind of play similar roles So that's more of a When you start writing Next you learn when it's Appropriate to use one Yeah I think the main problem I'm a function Well a procedural guard
32:40
So I'm a secret guard So I've got to get my head around a functional Way of thinking And that's the main kind of Everest Mountain thing so when you get Out of the functional curve Or strength form When you climb up that All this makes more sense Yeah you never muted anything right You only change
33:02
When you do operations Or only return new values From that Alright so I guess So when you code next It's a functional language It's lazy And it's pure And I just want to go over that quickly
33:23
So functional is This approach where You don't muted anything like we're talking about And you only Have functions to do Operations Lazy is because Next only does The operation when you access something
33:41
So you can have a whole Tree of operations If you don't touch it it's not going to Evaluate it so it's not like A scripting language where it's In your head when you think When you read the next code it's not It's not like It's not imperative where you do It does this thing then it does this thing
34:01
Then it does this thing It's only when you access the values That it's going to evaluate The whole thing Which is a bit But yeah Maybe I should demonstrate I can demonstrate more about this later And then finally
34:20
It's pure From everything I showed We are not writing anything To disk We are only reading things So there is no side effects For now all you can do From what I explained Is maybe Put a bunch of values together
34:41
And then produce a JSON Since I don't Is importing a file From a file system not The operation? It is but it's pure In the sense that you don't modify Anything As long as you only
35:00
Read it's fine Like an observer What happens if the file is not there? It's a foreign exception You get any errors When it's built It can happen It tries to tell you what All of the shell script just disappears
35:20
So the nice thing is If it fails it fails Consistently So when you compile it fails So then That's the next talk So right now All we are doing is Taking a bunch of values and putting them together
35:41
So that's The things that are The missing topics The big missing topic is the derivation Because that's where all the computation And the bits happen We are going to talk about this next So we have a pure Scripting function
36:02
A pure and functional Scripting image Hopefully now when you see Next code it's not just like What is the thing You can actually follow through Then from that Maybe you can It makes you more independent
36:21
Alright thanks That's it for now I wonder if it might be interesting to On that configuration.next to sort of unpack it So it's building each Attribute set individually Like you are using compact nested
36:41
Snacks Yeah It might be interesting to show The more verbose version I guess I can show
37:07
So we have the configuration Next here What should we do
37:21
I am just going to modify it for now And remove that it's not a function Because we established We don't need any of the Arguments in the way And now There is a Two code next and That allows you to actually read
37:41
The next code And you say And that returns The values But right now you can see It's lazy because it just returns The first step but it didn't try To get all of the rest Because we didn't access it
38:03
So we can say actually Please be strict And now it returns The whole thing So it's not really readable So I am just going to generate JSON Instead And now we have the word structure So on the left
38:21
So that's what we have On the left And here So the path is not a JSON value So it's
38:42
There is no such path right? There is no such path There is an error up there So we have the file Oh right So if I touch This Ah that's more than I did
39:08
So here we are starting To see a bit that It does magic Oh What's that? So what it does When it accesses the file
39:20
It imports it into the next star And that's the Value of the There is a hash here We are going to cover that later But if you change the configuration file It changes the hash This is for version right? For version Yeah
39:40
And another thing to notice Is that the keys are ordered And that's The properties that we want to have Because it's basically We want to be more repeatable So we need to do things like ordering things So you always get the same result Alright It's something we can count on
40:02
The keys will be ordered On the On the evaluation And the keys are ordered Alright I think that's enough for now Shall we take a small break And then we can go