!tDnwWRNkmmYtMXfaZl:nixos.org

Nix Language

1518 Members
Nix programming language267 Servers

Load older messages


SenderMessageTime
5 Sep 2024
@eya:catgirl.cloud@eya:catgirl.cloud left the room.22:38:59
6 Sep 2024
@tdjordan:matrix.orgThom Jordan joined the room.18:17:24
@mccartykim:matrix.orgKimberly McCarty joined the room.23:22:24
7 Sep 2024
@lunik1:lunik.onelunik1 changed their profile picture.01:45:32
@o-santi:matrix.orgLeonardo Santiago joined the room.02:15:50
@sliedes:hacklab.fiSami Liedes joined the room.03:27:48
@kotz:catgirl.cloudKotz left the room.08:09:38
@kotz:catgirl.cloudKotz joined the room.08:10:07
@throwachimera:matrix.orgthrowachimera Hi, I'm trying to learn Nix for NixOS and sometimes I want to evaluate expressions to understand them better. However, when I try using libraries from nixpkgs, they don't fully evaluate. For example:

nix-repl> lib.mkIf true "test"  
{ _type = "if"; condition = true; content = "test"; }

I'm not sure if this is due to lazy evaluation or if there’s some other reason. How can I work around this? In general, I'd be interested in hearing about the ways people here debug and learn the language, as I guess I'm not usually seeing the resulting attribute sets directly when using it to configure NixOS.
11:23:17
@eisfunke:eisfunke.comNicolas Lenz
In reply to @throwachimera:matrix.org
Hi, I'm trying to learn Nix for NixOS and sometimes I want to evaluate expressions to understand them better. However, when I try using libraries from nixpkgs, they don't fully evaluate. For example:

nix-repl> lib.mkIf true "test"  
{ _type = "if"; condition = true; content = "test"; }

I'm not sure if this is due to lazy evaluation or if there’s some other reason. How can I work around this? In general, I'd be interested in hearing about the ways people here debug and learn the language, as I guess I'm not usually seeing the resulting attribute sets directly when using it to configure NixOS.

This isn't actually something directly related to the language or evaluation, I think. The result in your example actually is fully evaluated! The mkIf funtion in nixpkgs is just defined in a way that evaluates to that attribute set.

The NixOS module system would tzen take that result if you put that in your config and use it to calculate yout final config. That is implemented in Nix, but not a feature of the Nix language itself. The Nix language has no concept of modules and so on, so NixOS uses things like Nix attribute sets with _type attribute as in your example to implement its features.

11:31:31
@emilazy:matrix.orgemilyyeah, I think the missing piece here is that the NixOS module system is functionality implemented entirely within the Nix language11:34:22
@emilazy:matrix.orgemilynot a language feature11:34:25
@emilazy:matrix.orgemilyyou'll have to call into the Nixpkgs library functions that resolve definitions, evaluate module configs, etc.11:34:44
@throwachimera:matrix.orgthrowachimera
In reply to@eisfunke:eisfunke.com

This isn't actually something directly related to the language or evaluation, I think. The result in your example actually is fully evaluated! The mkIf funtion in nixpkgs is just defined in a way that evaluates to that attribute set.

The NixOS module system would tzen take that result if you put that in your config and use it to calculate yout final config. That is implemented in Nix, but not a feature of the Nix language itself. The Nix language has no concept of modules and so on, so NixOS uses things like Nix attribute sets with _type attribute as in your example to implement its features.

Okay I see, I guess it makes sense. I'll dive a bit into the module system, thanks for the pointers!
11:39:05
@emilazy:matrix.orgemilythe Nixpkgs manual has some documentation on it11:42:45
@emilazy:matrix.orgemily and you can read lib/options.nix and lib/types.nix (but beware of dragons) 11:42:57
@infinisil:matrix.orginfinisil

throwachimera: Here's something that works:

nix-repl> moduleEval = type: config: lib.mergeDefinitions [ "root" ] type [ { file = "<expr>"; value = config; } ]

nix-repl> :p moduleEval lib.types.str (lib.mkIf true "test")                                                       
{ defsFinal = [ { file = "<expr>"; value = "test"; } ]; defsFinal' = { highestPrio = 100; values = [ «repeated» ]; }; isDefined = true; mergedValue = "test"; optionalValue = { value = "test"; }; }
11:50:33
@infinisil:matrix.orginfinisil

Or since .mergedValue is usually most interesting:

nix-repl> moduleEval = type: config: (lib.mergeDefinitions [ "root" ] type [ { file = "<expr>"; value = config; } ]).mergedValue

nix-repl> :p moduleEval lib.types.str (lib.mkIf true "test")                                                                     
"test"
11:52:09
@throwachimera:matrix.orgthrowachimera
In reply to@infinisil:matrix.org

Or since .mergedValue is usually most interesting:

nix-repl> moduleEval = type: config: (lib.mergeDefinitions [ "root" ] type [ { file = "<expr>"; value = config; } ]).mergedValue

nix-repl> :p moduleEval lib.types.str (lib.mkIf true "test")                                                                     
"test"
Ayy that's cool, thanks a lot :)! Man I still have such a long way to go, nix can be confusing 😅
11:54:08
@infinisil:matrix.orginfinisilGlad to help :)11:54:28
@infinisil:matrix.orginfinisilI'd say the Nix language is actually fairly simple. It's Nixpkgs that's so damn messy 😆11:54:56
@infinisil:matrix.orginfinisil Btw for more module system specific questions, there's also #modules:nixos.org 11:55:47
@nakibrayan5:matrix.org@nakibrayan5:matrix.org removed their profile picture.12:22:08
@nakibrayan5:matrix.org@nakibrayan5:matrix.org removed their display name ريان نقيب.12:22:14
@nakibrayan5:matrix.org@nakibrayan5:matrix.org left the room.12:22:21
@nakibrayan:tchncs.de@nakibrayan:tchncs.de joined the room.21:01:50
@nakibrayan:tchncs.de@nakibrayan:tchncs.de left the room.21:32:30
@pixelfog:matrix.orgpixelfog left the room.23:34:21
8 Sep 2024
@pixelfog:matrix.orgpixelfog joined the room.00:13:48
9 Sep 2024
@pandapip-1:matrix.orgPandapip1 joined the room.03:42:58

Show newer messages


Back to Room ListRoom Version: 6