!tDnwWRNkmmYtMXfaZl:nixos.org

Nix Language

1517 Members
Nix programming language267 Servers

Load older messages


SenderMessageTime
30 Jul 2024
@zm94zgv2:private.coffeeZm94ZGV2 changed their profile picture.13:02:00
@zm94zgv2:private.coffeeZm94ZGV2 changed their profile picture.13:04:57
@zm94zgv2:private.coffeeZm94ZGV2 changed their profile picture.13:06:30
@quapka4:matrix.orgquapka4

Is there a way to encode function call in JSON so that builtins.fromJSON would be able to parse it? Such as:

pkg = {
  v1 = functionCall { version = "1";};
}
14:23:58
@quasigod:matrix.org@quasigod:matrix.org left the room.15:08:00
@morgrimm:matrix.orgremi-gelinasWhy not just put that in a nix file and import it?16:00:27
@mattsturg:matrix.orgMatt Sturgeon

The problem here is that you are using part of users to define another part of users.

This is recursive, and because attrsOf is not lazy it is infinitely recursive.

You can either use types.lazyAttrsOf (which has its own gotchas) or refactor so that you aren't depending on users to define users.foo.

16:36:01
@infinisil:matrix.orginfinisil remi-gelinas: I'd guess because it's something e.g. auto-generated, or the JSON comes from an external source, etc. 20:51:28
@infinisil:matrix.orginfinisil
In reply to @quapka4:matrix.org

Is there a way to encode function call in JSON so that builtins.fromJSON would be able to parse it? Such as:

pkg = {
  v1 = functionCall { version = "1";};
}
Generally the answer is to have a Nix file that reads and transforms the JSON into what you need, e.g. lib.mapAttrs (name: value: functionCall value) (builtins.fromJSON (builtins.readFile ./foo.json))
20:52:14
@infinisil:matrix.orginfinisil
In reply to @quapka4:matrix.org

Is there a way to encode function call in JSON so that builtins.fromJSON would be able to parse it? Such as:

pkg = {
  v1 = functionCall { version = "1";};
}
* Generally the solution is to have a Nix file that reads and transforms the JSON into what you need, e.g. lib.mapAttrs (name: value: functionCall value) (builtins.fromJSON (builtins.readFile ./foo.json))
20:52:24
@infinisil:matrix.orginfinisilBut the answer to your question of whether it's possible to encode a function in JSON is definitely No :)20:52:43
@infinisil:matrix.orginfinisil * Generally you'll want to have a Nix file that reads and transforms the JSON into what you need, e.g. lib.mapAttrs (name: value: functionCall value) (builtins.fromJSON (builtins.readFile ./foo.json)) 20:53:00
@mattsturg:matrix.orgMatt Sturgeon *

Normally you would do this using the callPackage pattern where the various dependencies are inputs to your derivation function.

When you call your derivation using callPackage any arguments will default to pkgs.* if not explicitly passed in.

# A file using your package
{
  foo = pkgs.callPackage ./package.nix { /* explicit args (optional) */ };

  # This overrides a dep
  foobar = pkgs.callPackage ./package.nix {
    inherit some-dep;
  };
}
# package.nix
{
  lib,
  runCommand,
  some-dep,
  other-dep,
}:
runCommand "name" {} ''
  echo "Hello, world!" > "$out"
''
21:12:03
31 Jul 2024
@xavierlauzon:matrix.orgxavierlauzon joined the room.02:27:55
@quapka4:matrix.orgquapka4
In reply to @infinisil:matrix.org
But the answer to your question of whether it's possible to encode a function in JSON is definitely No :)
I see, thanks. As you have guessed correctly, I am automatically generating "the JSON" from Python. For now, I settled on using Jinja2 template to directly generate Nix files.
10:08:44
@quapka4:matrix.orgquapka4

Somewhat related, when I output many packages (those auto-generated), can I force nix flake show to show nested outputs? For example:

 # flake.nix
...
  outputs = ...
  {
    packages = {
      pkg = {
        v1 = builder { version = "1"; };
        v2 = builder { version = "2"; };
      };
    };

And then have nix flake show display also pkg.v1, pkg.v2 etc.

10:12:49
@quapka4:matrix.orgquapka4
In reply to @infinisil:matrix.org
Generally you'll want to have a Nix file that reads and transforms the JSON into what you need, e.g. lib.mapAttrs (name: value: functionCall value) (builtins.fromJSON (builtins.readFile ./foo.json))
I haven't thought about the mapping, that is interesting, I'll see if that would make sense.
10:14:52
@quapka4:matrix.orgquapka4
In reply to @quapka4:matrix.org

Somewhat related, when I output many packages (those auto-generated), can I force nix flake show to show nested outputs? For example:

 # flake.nix
...
  outputs = ...
  {
    packages = {
      pkg = {
        v1 = builder { version = "1"; };
        v2 = builder { version = "2"; };
      };
    };

And then have nix flake show display also pkg.v1, pkg.v2 etc.

Simply, specify the level of the printed trees.
10:19:57
@quapka4:matrix.orgquapka4Also, are there Python bindings to write/read Nix files?10:41:22
@ity:itycodes.orgTranquil Ity
In reply to @quapka4:matrix.org
Also, are there Python bindings to write/read Nix files?
What do you mean by write/read Nix files? Like a parser to spit out an AST?
10:45:44
@ity:itycodes.orgTranquil Ity* What do you mean by write/read Nix files? Like a parser to spit out an AST as objects?10:45:59
@quapka4:matrix.orgquapka4Yes, something like BeautifulSoup for HTML, but for Nix files.10:47:02
@ity:itycodes.orgTranquil ItyWell, HTML is a markup language while Nix is a programming language. All I can find is https://github.com/nix-community/rnix-parser in Rust, you can probably bind it to Python in some way.11:04:25
@quapka4:matrix.orgquapka4
In reply to @ity:itycodes.org
Well, HTML is a markup language while Nix is a programming language. All I can find is https://github.com/nix-community/rnix-parser in Rust, you can probably bind it to Python in some way.
Nice, thanks.
11:10:39
@cynerd:matrix.orgcynerd left the room.11:53:02
@cynerd:matrix.orgcynerd joined the room.11:53:50
@thalos:jupiterbroadcasting.com@thalos:jupiterbroadcasting.com left the room.17:34:56
1 Aug 2024
@ibtalot:matrix.org@ibtalot:matrix.org left the room.04:50:08
@thescientiac:matrix.org@thescientiac:matrix.org left the room.05:15:58
@aneesh:aneeshusa.comaneeshusa joined the room.05:18:51

Show newer messages


Back to Room ListRoom Version: 6