Skip to content

Commit

Permalink
Validate devenv.yaml schema and move overlays to inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Feb 9, 2023
1 parent 4430eb1 commit b0f22d7
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/reference/yaml-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
| inputs.<name> | Identifier name used when passing the input in your ``devenv.nix`` function. |
| inputs.<name>.url | URI specification of the input, see below for possible values. |
| inputs.<name>.flake | Does the input contain ``flake.nix`` or ``devenv.nix``. Defaults to ``true``. |
| inputs.<name>.overlays | A list of overlays to include from the input. |
| imports | A list of relative paths or references to inputs to import ``devenv.nix``. |
| overlays | Names of inputs with a list of overlays to include. |


## inputs.<name>.url

Expand Down
7 changes: 3 additions & 4 deletions examples/overlays/devenv.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
allowUnfree: true
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/nixpkgs-unstable
rust-overlay:
url: github:oxalica/rust-overlay
allowUnfree: true
overlays:
rust-overlay:
- default
overlays:
- default
43 changes: 43 additions & 0 deletions src/devenv-yaml.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{ pkgs }:

pkgs.writers.writePython3Bin "devenv-yaml" { libraries = with pkgs.python3Packages; [ strictyaml path ]; } ''
from strictyaml import Map, MapPattern, Str, Seq
from strictyaml import load, Bool, Any, Optional, YAMLError
import json
import sys
import os
from path import Path
inputsSchema = MapPattern(Str(), Map({
"url": Str(),
Optional("flake", default=None): Bool(),
Optional("inputs", default=None): Any(),
Optional("overlays", default=None): Seq(Str())
}))
schema = Map({
Optional("inputs", default=None): inputsSchema,
Optional("allowUnfree", default=False): Bool(),
Optional("imports", default=None): Seq(Str())
})
filename = Path("devenv.yaml").bytes().decode('utf8')
try:
devenv = load(filename, schema, label="devenv.yaml").data
except YAMLError as error:
print("Error in `devenv.yaml`", error)
sys.exit(1)
inputs = {}
for input, attrs in devenv['inputs'].items():
inputs[input] = {k: attrs[k] for k in ('url', 'inputs', 'flake')
if k in attrs}
devenv_state = sys.argv[1]
with open(os.path.join(devenv_state, "flake.json"), 'w') as f:
f.write(json.dumps(inputs))
with open(os.path.join(devenv_state, "devenv.json"), 'w') as f:
f.write(json.dumps(devenv))
''
4 changes: 2 additions & 2 deletions src/devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ pkgs.writeScriptBin "devenv" ''
export DEVENV_DIR="$(pwd)/.devenv"
export DEVENV_GC="$DEVENV_DIR/gc"
mkdir -p "$DEVENV_GC"
# TODO: validate devenv.yaml using jsonschema
if [[ -f devenv.yaml ]]; then
cat devenv.yaml | ${pkgs.yaml2json}/bin/yaml2json > "$DEVENV_DIR/devenv.json"
${import ./devenv-yaml.nix { inherit pkgs; }}/bin/devenv-yaml "$DEVENV_DIR"
else
[[ -f "$DEVENV_DIR/devenv.json" ]] && rm "$DEVENV_DIR/devenv.json"
[[ -f "$DEVENV_DIR/flake.json" ]] && rm "$DEVENV_DIR/flake.json"
fi
cp -f ${import ./flake.nix { inherit pkgs version; }} "$FLAKE_FILE"
chmod +w "$FLAKE_FILE"
Expand Down
13 changes: 7 additions & 6 deletions src/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
devenv.url = "github:cachix/devenv?dir=src/modules";
} // (if builtins.pathExists ./.devenv/devenv.json
then (builtins.fromJSON (builtins.readFile ./.devenv/devenv.json)).inputs
} // (if builtins.pathExists ./.devenv/flake.json
then builtins.fromJSON (builtins.readFile ./.devenv/flake.json)
else {});
outputs = { nixpkgs, ... }@inputs:
let
devenv = if builtins.pathExists ./.devenv/devenv.json
then builtins.fromJSON (builtins.readFile ./.devenv/devenv.json)
else {};
getOverlays = inputName: overlays:
map (overlay: let
getOverlays = inputName: inputAttrs:
map (overlay: let
input = inputs.''${inputName} or (throw "No such input `''${inputName}` while trying to configure overlays.");
in input.overlays.''${overlay} or (throw "Input `''${inputName}` has no overlay called `''${overlay}`. Supported overlays: ''${nixpkgs.lib.concatStringsSep ", " (builtins.attrNames input.overlays)}")) overlays;
overlays = nixpkgs.lib.flatten (nixpkgs.lib.mapAttrsToList getOverlays (devenv.overlays or {}));
in input.overlays.''${overlay} or (throw "Input `''${inputName}` has no overlay called `''${overlay}`. Supported overlays: ''${nixpkgs.lib.concatStringsSep ", " (builtins.attrNames input.overlays)}"))
inputAttrs.overlays or [];
overlays = nixpkgs.lib.flatten (nixpkgs.lib.mapAttrsToList getOverlays (devenv.inputs or {}));
pkgs = import nixpkgs {
system = "${pkgs.system}";
allowUnfree = devenv.allowUnfree or false;
Expand Down

0 comments on commit b0f22d7

Please sign in to comment.