From 9814ddd9fc2d557abd589bb82bd2a654ea9bc7fb Mon Sep 17 00:00:00 2001 From: pegasust Date: Thu, 8 Dec 2022 14:00:53 -0700 Subject: [PATCH] nix: componentize fromYaml onto lib --- nix-conf/home-manager/flake.lock | 17 ----------------- nix-conf/home-manager/flake.nix | 7 +------ nix-conf/home-manager/home.nix | 16 ++-------------- nix-conf/lib/default.nix | 12 ++++++++---- nix-conf/lib/serde/default.nix | 28 ++++++++++++++++++++++++++++ scripts/hm-switch.sh | 2 +- 6 files changed, 40 insertions(+), 42 deletions(-) create mode 100644 nix-conf/lib/serde/default.nix diff --git a/nix-conf/home-manager/flake.lock b/nix-conf/home-manager/flake.lock index c58fdee..36a6e70 100644 --- a/nix-conf/home-manager/flake.lock +++ b/nix-conf/home-manager/flake.lock @@ -45,22 +45,6 @@ "type": "github" } }, - "from-yaml": { - "flake": false, - "locked": { - "lastModified": 1667993187, - "narHash": "sha256-JG1XYGCmR7Jqawvs8AO4c2mrOl37BXGGgU9cfeL0b8k=", - "owner": "pegasust", - "repo": "fromYaml", - "rev": "ef0ed4acfc4a6b7a0f1515cb48f5dbdda89dac3c", - "type": "github" - }, - "original": { - "owner": "pegasust", - "repo": "fromYaml", - "type": "github" - } - }, "home-manager": { "inputs": { "nixpkgs": [ @@ -151,7 +135,6 @@ "root": { "inputs": { "flake-utils": "flake-utils", - "from-yaml": "from-yaml", "home-manager": "home-manager", "nixgl": "nixgl", "nixpkgs": "nixpkgs_2", diff --git a/nix-conf/home-manager/flake.nix b/nix-conf/home-manager/flake.nix index be0b22b..40a727d 100644 --- a/nix-conf/home-manager/flake.nix +++ b/nix-conf/home-manager/flake.nix @@ -9,10 +9,6 @@ flake-utils.url = "github:numtide/flake-utils"; nixgl.url = "github:guibou/nixGL"; rust-overlay.url = "github:oxalica/rust-overlay"; - from-yaml = { - url = "github:pegasust/fromYaml"; - flake = false; - }; }; outputs = @@ -21,7 +17,6 @@ , nixgl , rust-overlay , flake-utils - , from-yaml , ... }: let @@ -32,7 +27,7 @@ inherit system overlays; config = { allowUnfree = true; }; }; - lib = (import ../lib-nix { inherit pkgs from-yaml; lib = pkgs.lib; }); + lib = (import ../lib { inherit pkgs; lib = pkgs.lib; }); in { homeConfigurations = diff --git a/nix-conf/home-manager/home.nix b/nix-conf/home-manager/home.nix index 5b11657..5944703 100644 --- a/nix-conf/home-manager/home.nix +++ b/nix-conf/home-manager/home.nix @@ -2,6 +2,7 @@ { config , pkgs # This is by default just ``= import {}` , myHome +, myLib , ... }: let @@ -28,20 +29,7 @@ let # ])) ]; proj_root = builtins.toString ./../..; - # TODO: put this in a seperate library - # callPackage supports both PATH and function as first param! - yamlToJsonDrv = yamlContent: outputPath: pkgs.callPackage - ({ runCommand }: - # runCommand source: https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix#L33 - runCommand outputPath { inherit yamlContent; nativeBuildInputs = [ pkgs.yq ]; } - # run yq which outputs '.' (no filter) on file at yamlPath - # note that $out is passed onto the bash/sh script for execution - '' - echo "$yamlContent" | yq >$out - '') - { }; - # fromYamlPath = yamlPath: builtins.fromJSON (builtins.readFile (yamlToJsonDrv yamlPath "any-output.json")); - fromYaml = yamlContent: builtins.fromJSON (builtins.readFile (yamlToJsonDrv yamlContent "any_output.json")); + inherit (myLib) fromYaml; in { home = { diff --git a/nix-conf/lib/default.nix b/nix-conf/lib/default.nix index 7e564bf..6876ca0 100644 --- a/nix-conf/lib/default.nix +++ b/nix-conf/lib/default.nix @@ -1,5 +1,9 @@ -{pkgs, lib, from-yaml, ...}@flake_import: -{ - fromYaml = import "${from-yaml}/fromYaml.nix" {inherit lib;}; - +{ pkgs +, lib +, ... }@flake_import: +let serde = import ./serde { inherit pkgs lib; }; +in +pkgs.lib // lib // { + fromYaml = serde.fromYaml; + fromYamlPath = serde.fromYamlPath; } diff --git a/nix-conf/lib/serde/default.nix b/nix-conf/lib/serde/default.nix new file mode 100644 index 0000000..b29166b --- /dev/null +++ b/nix-conf/lib/serde/default.nix @@ -0,0 +1,28 @@ +# Takes care of serializing and deserializing to some formats +# Blame: Pegasust +# TODO: Add to* formats from pkgs.formats.* +{ pkgs +, lib +} @ inputs: +let + yamlToJsonDrv = yamlContent: outputPath: pkgs.callPackage + ({ runCommand }: + # runCommand source: https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/trivial-builders.nix#L33 + runCommand outputPath { inherit yamlContent; nativeBuildInputs = [ pkgs.yq ]; } + # run yq which outputs '.' (no filter) on file at yamlPath + # note that $out is passed onto the bash/sh script for execution + '' + echo "$yamlContent" | yq >$out + '') + { }; +in { + # Takes in a yaml string and produces a derivation with translated JSON at $outputPath + # similar to builtins.fromJSON, turns a YAML string to nix attrset + fromYaml = yamlContent: builtins.fromJSON (builtins.readFile (yamlToJsonDrv yamlContent "any_output.json")); + fromYamlPath = yamlPath: builtins.fromJSON ( + builtins.readFile ( + yamlToJsonDrv ( + builtins.readFile yamlPath) + "any-output.json")); + # TODO: fromToml? +} diff --git a/scripts/hm-switch.sh b/scripts/hm-switch.sh index bfafbed..fb0614d 100755 --- a/scripts/hm-switch.sh +++ b/scripts/hm-switch.sh @@ -10,7 +10,7 @@ HOME_MANAGER_DIR="${SCRIPT_DIR}/../nix-conf/home-manager" # test if we have home-manager, if not, attempt to use nix to put home-manager to # our environment -if [ $(home-manager &>/dev/null) ]; then +if [ $(home-manager >/dev/null 2>&1) ]; then nix-shell -p home-manager --run "home-manager switch --flake $HOME_MANAGER_DIR" else home-manager switch --flake "$HOME_MANAGER_DIR"