!tDnwWRNkmmYtMXfaZl:nixos.org

Nix Language

1519 Members
Nix programming language268 Servers

Load older messages


SenderMessageTime
21 Sep 2024
@iv-nn:matrix.orgiv-nn joined the room.18:17:25
22 Sep 2024
@cafkafk:gitter.imcafkafk
In reply to @wizardlink:matrix.org

Hello there! A quick and maybe dumb question...

I am currently working on my home-manager configuration and decided to make my scripts into .nix files that evaluate strings (for interpolating packages). Now, if I do

{
  # ...
  text = import ./my-script.nix;
}

It yields an error, which is very non-descriptive... But:

{
  # ...
  text = import ./my-script.nix { pkgs = pkgs; };
}

Does not errors and the function is evaluated? I thought that parameters got passed down kind of like imports does, but it isn't the case... Can someone explain me better how functions are evaluated in the case of an import?

Thanks in advance!

imports is actually a nixpkgs/nixos specific thing, it calls the "modules" (really functions) with a bunch of pre-defined arguments (see e.g. nixpkgs/lib/modules.nix if curious).

import on the other hand is a Nix keyword that parses and loads the expression from some path, and the attrSet it takes here (in your case { pkgs = pkgs; } is an argument passed to the function found in ./my-script.nix.

the start of your ./my-script.nix file likely looks something like this:

{ pkgs }:

Here, { pkgs }: is actually an anonymous function taking an attrSet with an attribute called pkgs.

09:29:41
@cafkafk:gitter.imcafkafk The reason you can't just use imports ./my-script.nix is because you're not specifying the pkgs attribute 09:31:40
@cafkafk:gitter.imcafkafk * The reason you can't just use import ./my-script.nix is because you're not specifying the pkgs attribute 09:31:44
@cafkafk:gitter.imcafkafk * The reason you can't just use import ./my-script.nix is because you're not specifying the pkgs attribute as an argument 09:32:05
@aos1:matrix.orgaos What are the odds that we're both answering this question around the same time haha. Your answer is more thorough cafkafk thanks. I do want to add some links in case it's helpful: https://nix.dev/manual/nix/2.18/language/builtins.html?highlight=import#builtins-import and https://nixos.org/manual/nixos/stable/#sec-writing-modules 09:33:19
@cafkafk:gitter.imcafkafk
In reply to @aos1:matrix.org
What are the odds that we're both answering this question around the same time haha. Your answer is more thorough cafkafk thanks. I do want to add some links in case it's helpful: https://nix.dev/manual/nix/2.18/language/builtins.html?highlight=import#builtins-import and https://nixos.org/manual/nixos/stable/#sec-writing-modules
Hehe, thanks :p
09:33:35
@cafkafk:gitter.imcafkafk Also if you do wanna evaluate something like it's a NixOS module instead of using import, I'm pretty sure you can use lib.evalModules to get something like imports 09:34:40
@cafkafk:gitter.imcafkafk

ohh also, turns out import isn't technically a keyword, but a builtin ๐Ÿ™ƒ

nix-repl> import = x: x + 1

nix-repl> import 10
11
09:37:02
@rayne:spooky.computer@rayne:spooky.computer left the room.10:25:42
@me:caem.devcaem changed their profile picture.19:34:01
23 Sep 2024
@kamillaova:matrix.orgKamilla 'ova
In reply to@cafkafk:gitter.im

ohh also, turns out import isn't technically a keyword, but a builtin ๐Ÿ™ƒ

nix-repl> import = x: x + 1

nix-repl> import 10
11
null = "definitely not null" moment
02:48:22
@cafkafk:gitter.imcafkafk I think it's a good argument for always using builtins.<thing> which I haven't managed to reassign (yet) 02:50:10
@cafkafk:gitter.imcafkafkokay well you can reassign builtins but02:50:22
@cafkafk:gitter.imcafkafk * I think it's a good argument for always using builtins.<thing> which I haven't managed to reassign (yet) 02:51:17
@rz_mj:freiburg.social@rz_mj:freiburg.social left the room.07:33:37
@federicodschonborn:matrix.orgFederico Damiรกn Schonborn joined the room.12:47:10
@elikoga:matrix.orgelikoga joined the room.15:29:22
@beleap:matrix.orgBeLeap changed their profile picture.20:30:03
@beleap:matrix.orgBeLeap changed their display name from beleap to BeLeap.20:30:18
@beleap:matrix.orgBeLeap changed their profile picture.20:33:08
@me:caem.devcaem changed their profile picture.21:57:51
24 Sep 2024
@kuznero:matrix.orgkuznero joined the room.09:45:03
@memegames99:matrix.org@memegames99:matrix.org
In reply to @cafkafk:gitter.im

ohh also, turns out import isn't technically a keyword, but a builtin ๐Ÿ™ƒ

nix-repl> import = x: x + 1

nix-repl> import 10
11
very cool
10:14:22
@keithhub:matrix.orgkeithhub joined the room.18:04:56
@mel:rnrd.euMel joined the room.19:00:58
@ckie:ckie.devmei ๐ŸŒ’& changed their profile picture.23:24:59
25 Sep 2024
@luna-null:matrix.orgluna-null changed their display name from luna-null to Autumn.06:39:15
@artturin:matrix.orgArtturin
arch = builtins.getAttr stdenvNoCC.hostPlatform.system {
  x86_64-linux = "x64";
  x86-linux = "x86";
};

vs

arch =
  {
    x86_64-linux = "x64";
    x86-linux = "x86";
  }
  ."${stdenvNoCC.hostPlatform.system}";
20:55:59
@artturin:matrix.orgArtturinAny perf diff?20:56:31

Show newer messages


Back to Room ListRoom Version: 6