Skip to content

Commit

Permalink
refactor: refactors nix-cue into the internal lib
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed May 27, 2022
1 parent 07ddd97 commit 3a82171
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
24 changes: 0 additions & 24 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
nix-cue.url = "github:jmgilman/nix-cue";
nix-cue.inputs.nixpkgs.follows = "nixpkgs";
nix-cue.inputs.flake-utils.follows = "flake-utils";
};

outputs = { self, nixpkgs, flake-utils, nix-cue }:
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
Expand Down Expand Up @@ -52,7 +49,7 @@
in
{
# Load lib functions
lib = (import ./lib { inherit pkgs lib; }) // { nix-cue = nix-cue.lib.${system}; };
lib = (import ./lib { inherit pkgs lib; });

# Load plugins
plugins = import ./plugins { inherit pkgs lib; };
Expand Down
2 changes: 2 additions & 0 deletions lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{ pkgs, lib }:
{
eval = import ./eval.nix { inherit pkgs lib; };

/* Combines the shellHook from multiple configurations into one.
*/
mkShellHook = configs: builtins.concatStringsSep "\n" (pkgs.lib.catAttrs "shellHook" configs);
Expand Down
34 changes: 34 additions & 0 deletions lib/eval.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Evaluates the given input files and data and returns a derivation which builds the result.
*/
{ pkgs, lib }:
{ inputFiles, outputFile, data ? { }, cue ? pkgs.cue, ... }@args:
with pkgs.lib;
let
json = optionalString (data != { }) (builtins.toJSON data);

defaultFlags = {
outfile = "$out";
};
extraFlags = removeAttrs args [ "inputFiles" "outputFile" "data" "cue" ];

allFlags = defaultFlags // extraFlags;
allInputs = inputFiles ++ optionals (json != "") [ "json: $jsonPath" ];

flagsToString = name: value: if (builtins.isBool value) then "--${name}" else ''--${name} "${value}"'';
flagStr = builtins.concatStringsSep " " (attrValues (mapAttrs flagsToString allFlags));
inputStr = builtins.concatStringsSep " " allInputs;
cueEvalCmd = "cue eval ${flagStr} ${inputStr}";

result = pkgs.runCommand outputFile
({
inherit json;
buildInputs = [ cue ];
passAsFile = [ "json" ];
} // optionalAttrs (json != "") { inherit json; passAsFile = [ "json" ]; })
''
echo "nixago: Rendering output..."
${cueEvalCmd}
'';
in
result
2 changes: 1 addition & 1 deletion lib/template.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let
inherit data files output shellHookExtra;
}
];
specialArgs = ({ inherit (lib) nix-cue; inherit pkgs; } // flags);
specialArgs = ({ inherit pkgs; flakeLib = lib; } // flags);
};
in
result.config
Expand Down
7 changes: 3 additions & 4 deletions modules/template.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
option can be set to define extra shell commands to run when the configuration
file is regenerated due to the underlying data changing.
*/
{ config, lib, nix-cue, pkgs, ... }@args:
{ config, lib, flakeLib, pkgs, ... }@args:
with pkgs.lib;
let
inherit (lib) mkOption types;
flags = removeAttrs args [ "config" "lib" "nix-cue" "pkgs" "options" "specialArgs" ];
flags = removeAttrs args [ "config" "lib" "flakeLib" "pkgs" "options" "specialArgs" ];
hook = ''
# Check if the link is pointing to the existing derivation result
if readlink ${config.output} >/dev/null \
Expand All @@ -41,9 +41,8 @@ in
configFile = mkOption {
type = types.package;
description = "The generated configuration file";
default = nix-cue.eval
default = flakeLib.eval
({
inherit pkgs;
inputFiles = config.files;
outputFile = config.output;
data = config.data;
Expand Down

0 comments on commit 3a82171

Please sign in to comment.