-
-
Save cmacrae/0fd174decdfe1320f8e72dd0a051cdca to your computer and use it in GitHub Desktop.
{ hostName }: | |
with (import <nixpkgs>); | |
let | |
baseSet = { | |
baseAttr = { | |
key1 = "foo"; | |
key2 = "bar"; | |
}; | |
anotherBaseAttr = { | |
key1 = "baz"; | |
key3 = "bang"; | |
}; | |
}; | |
in baseSet // lib.optionalAttrs (hostName == "imac") { my = "set"; } |
xdg.configFile."test.json" = { | |
text = builtins.toJSON (import /tmp/play/attrs.nix { inherit (config.networking) hostName; }); | |
}; |
error: value is a function while a set was expected |
If you only use hostName in here, you might just pass the hostname to this file and convert it back to a simple nix expr. :-)
{ hostName }:
let
# ....
in baseSet // optionalAttrs (hostName == "imac") { my = "set"; }
you could then from your config: result = builtins.toJSON (import ./example.nix { inherit (config.networking) hostName; });
-
You need
lib
inattrs.nix
. You could just pass it along as second param, like:(import /tmp/play/attrs.nix { inherit (config.networking) hostName; inherit lib; })
and make sure attrs.nix accepts{ hostName, lib }:
. This, IMO, is better than importing a whole new pkgs-set. If you do so, you can removewith (import <nixpkgs>);
altogether. -
At the location of import, you might want to use a trivial builder to write out a json file to the nix-store, something like:
configFile = pkgs.writeText "test.json" (builtins.toJSON ( ... ));
-
I'd still place brackets around
(lib.optionalAttrs (hostName == "imac") { my = "set"; })
and then from your existing nixos configuration, do something like:
imports = [ ./example.nix ];