From 947a7d33f997d8782ae74e2e76fb7645e86b663e Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sun, 16 Aug 2020 15:39:52 +0200 Subject: lib: add importTOML Complements the `lib.importJSON`. `builtins.readTOML` has been introduced in Nix 2.1. --- lib/default.nix | 2 +- lib/trivial.nix | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/default.nix b/lib/default.nix index 43b9ab5930c4..44076d295176 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -66,7 +66,7 @@ let stringLength sub substring tail; inherit (trivial) id const pipe concat or and bitAnd bitOr bitXor bitNot boolToString mergeAttrs flip mapNullable inNixShell min max - importJSON warn info showWarnings nixpkgsVersion version mod compare + importJSON importTOML warn info showWarnings nixpkgsVersion version mod compare splitByAndCompare functionArgs setFunctionArgs isFunction toHexString toBaseDigits; inherit (fixedPoints) fix fix' converge extends composeExtensions makeExtensible makeExtensibleWithCustomName; diff --git a/lib/trivial.nix b/lib/trivial.nix index 9501a2906cae..268f39d32103 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -281,6 +281,12 @@ rec { importJSON = path: builtins.fromJSON (builtins.readFile path); + /* Reads a TOML file. + + Type :: path -> any + */ + importTOML = path: + builtins.fromTOML (builtins.readFile path); ## Warnings -- cgit v1.2.3 From 035627dff23c4524345c4013e5e01ca95597452b Mon Sep 17 00:00:00 2001 From: zimbatm Date: Sat, 12 Sep 2020 16:33:56 +0200 Subject: lib: allow to import JSON and TOML files The vision here is that configuration tools can generate .json or .toml files, which can be plugged into an existing configuration. Eg: { lib, ... }: { imports = [ (lib.modules.importJSON ./hardware-configuration.json) ]; } --- lib/modules.nix | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib') diff --git a/lib/modules.nix b/lib/modules.nix index 412c7f1df712..e7012742a982 100644 --- a/lib/modules.nix +++ b/lib/modules.nix @@ -869,4 +869,21 @@ rec { ]; }; + /* Use this function to import a JSON file as NixOS configuration. + + importJSON -> path -> attrs + */ + importJSON = file: { + _file = file; + config = lib.importJSON file; + }; + + /* Use this function to import a TOML file as NixOS configuration. + + importTOML -> path -> attrs + */ + importTOML = file: { + _file = file; + config = lib.importTOML file; + }; } -- cgit v1.2.3