Skip to content

Instantly share code, notes, and snippets.

@cmacrae
Last active April 9, 2019 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmacrae/0fd174decdfe1320f8e72dd0a051cdca to your computer and use it in GitHub Desktop.
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
@johanot
Copy link

johanot commented Apr 9, 2019

{ config, pkgs, lib, ... }:
   with lib;

let
  baseSet = {
    baseAttr = {
      key1 = "foo";
      key2 = "bar";
    };

    anotherBaseAttr = {
      key1 = "baz";
      key3 = "bang";
    };
  };

in baseSet // optionalAttrs (config.networking.hostName == "imac") { my = "set"; }

and then from your existing nixos configuration, do something like: imports = [ ./example.nix ];

@johanot
Copy link

johanot commented Apr 9, 2019

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; });

@johanot
Copy link

johanot commented Apr 9, 2019

  1. You need lib in attrs.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 remove with (import <nixpkgs>); altogether.

  2. 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 ( ... ));

  3. I'd still place brackets around (lib.optionalAttrs (hostName == "imac") { my = "set"; })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment