Skip to content

Commit

Permalink
devenv inputs add <name> <url>
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Jul 27, 2023
1 parent 379332a commit 4d227ef
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 86 deletions.
35 changes: 34 additions & 1 deletion src/devenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import terminaltables


from .yaml import validate_and_parse_yaml
from .yaml import validate_and_parse_yaml, read_yaml, write_yaml
from .log import log, log_task


Expand Down Expand Up @@ -66,6 +66,7 @@ def run_command(command: str) -> str:
if e.returncode == 130:
pass # we're exiting the shell
else:
click.echo("\n", err=True)
log(f"Following command exited with code {e.returncode}:\n\n {e.cmd}", level="error")
exit(e.returncode)

Expand Down Expand Up @@ -457,3 +458,35 @@ def print_dev_env(ctx):
def get_version():
with open(Path(MODULES_DIR, "latest-version")) as f:
return f.read().strip()

@cli.group(
help="Manage inputs in devenv.yaml. See http://devenv.sh/inputs/",
short_help="Manage inputs in devenv.yaml. See http://devenv.sh/inputs/"
)
def inputs():
pass

@inputs.command(
help="Add a new input to the developer environment.",
short_help="Add a new input to the developer environment.",
)
@click.argument('name')
@click.argument('url')
@click.option('--follows', '-f', multiple=True, help='Add a dependency to the input.')
@click.pass_context
def add(ctx, name, url, follows):
devenv = read_yaml()
attrs = {'url': url}

inputs = {}
for follow in follows:
if follow not in devenv['inputs']:
log(f"Input {follow} does not exist so it can't be followed.", level="error")
exit(1)
inputs[follow] = {"follows": follow}

if inputs:
attrs['inputs'] = inputs
devenv['inputs'][name] = attrs

write_yaml(devenv)
15 changes: 12 additions & 3 deletions src/devenv/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@
Optional("permittedInsecurePackages", default=None): Seq(Str())
})

def validate_and_parse_yaml(dot_devenv_root):
YAML_FILE = Path("devenv.yaml")

def read_yaml():
try:
with open(Path("devenv.yaml")) as f:
devenv = load(f.read(), schema, label="devenv.yaml").data
with open(YAML_FILE) as f:
return load(f.read(), schema, label="devenv.yaml")
except YAMLError as error:
print("Validation error in `devenv.yaml`", error)
sys.exit(1)

def write_yaml(yaml):
with open(YAML_FILE, "w") as f:
f.write(yaml.as_yaml())

def validate_and_parse_yaml(dot_devenv_root):
devenv = read_yaml().data

inputs = {}
for input, attrs in devenv.get('inputs', {}).items():
inputs[input] = {k: attrs[k] for k in ('url', 'inputs', 'flake')
Expand Down
25 changes: 13 additions & 12 deletions src/modules/containers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ let
if config.name == null
then throw ''You need to set `name = "myproject";` or `containers.${name}.name = "mycontainer"; to be able to generate a container.''
else config.name;
setup = ''
inputs:
nix2container:
url: github:nlewo/nix2container
inputs:
nixpkgs:
follows: nixpkgs
mk-shell-bin:
url: github:rrbutani/nix-mk-shell-bin
'';
types = lib.types;
envContainerName = builtins.getEnv "DEVENV_CONTAINER";
nix2containerInput = inputs.nix2container or (throw "To build the container, you need to add the following to your devenv.yaml:\n\n${setup}");
devenvlib = import ./devenv-lib.nix { inherit pkgs config inputs lib; };

nix2containerInput = devenvlib.getInput {
name = "nix2container";
url = "github:nlewo/nix2container";
attribute = "containers";
follows = [ "nixpkgs" ];
};
nix2container = nix2containerInput.packages.${pkgs.stdenv.system};
mk-shell-bin = inputs.mk-shell-bin or (throw "To build the container, you need to add the following to your devenv.yaml:\n\n${setup}");
mk-shell-bin = devenvlib.getInput {
name = "mk-shell-bin";
url = "github:rrbutani/nix-mk-shell-bin";
attribute = "containers";
};
shell = mk-shell-bin.lib.mkShellBin { drv = config.shell; nixpkgs = pkgs; };
mkEntrypoint = cfg: pkgs.writeScript "entrypoint" ''
#!${pkgs.bash}/bin/bash
Expand Down
27 changes: 27 additions & 0 deletions src/modules/devenv-lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ pkgs, lib, config, inputs }:

{
getInput = { name, url, attribute, follows ? [ ] }:
let
flags = lib.concatStringsSep " " (map (i: "--follows ${i}") follows);
yaml_follows = lib.concatStringsSep "\n " (map (i: "${i}:\n follows: ${i}") follows);
command =
if lib.versionAtLeast config.devenv.cliVersion "1.0"
then ''
run the following command:
$ devenv inputs add ${name} ${url} ${flags}
''
else ''
add the following to your devenv.yaml:
✨ devenv 1.0 made this easier: https://devenv.sh/getting-started/#installation ✨
inputs:
${name}:
url: ${url}
${if follows != [] then "inputs:\n ${yaml_follows}" else ""}
'';
in
inputs.${name} or (throw "To use '${attribute}', ${command}\n\n");
}
32 changes: 2 additions & 30 deletions src/modules/languages/gleam.nix
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
{ pkgs, config, lib, inputs, ... }:
{ pkgs, config, lib, ... }:

let
cfg = config.languages.gleam;

setup = ''
To use gleam, you need to add the following to your devenv.yaml:
inputs:
gleam-nix:
url: github:vic/gleam-nix
overlays:
- default
Optionally, if you want a specific gleam branch or version, do
the following:
inputs:
gleam:
url: github:gleam-lang/gleam/main # or any other branch
flake: false
gleam-nix:
url: github:vic/gleam-nix
overlays:
- default
inputs:
gleam = "gleam"
'';

gleamPkg = pkgs.gleam or (throw setup);

in
{
options.languages.gleam = {
enable = lib.mkEnableOption "tools for Gleam development";

package = lib.mkOption {
type = lib.types.package;
default = gleamPkg;
default = pkgs.gleam;
description = "The Gleam package to use.";
defaultText = lib.literalExpression "pkgs.gleam";
};
Expand Down
18 changes: 8 additions & 10 deletions src/modules/languages/php.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
with lib;

let
inherit (lib.attrsets) attrValues genAttrs;
inherit (lib.attrsets) attrValues;

cfg = config.languages.php;

setup = ''
inputs:
phps:
url: github:fossar/nix-phps
inputs:
nixpkgs:
follows: nixpkgs
'';
devenvlib = import ../devenv-lib.nix { inherit pkgs config inputs lib; };

phps = inputs.phps or (throw "To use languages.php.version, you need to add the following to your devenv.yaml:\n\n${setup}");
phps = devenvlib.getInput {
name = "phps";
url = "github:fossar/nix-phps";
attribute = "languages.php.version";
follows = [ "nixpkgs" ];
};

filterDefaultExtensions = ext: builtins.length (builtins.filter (inner: inner == ext.extensionName) cfg.disableExtensions) == 0;

Expand Down
12 changes: 6 additions & 6 deletions src/modules/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
let
cfg = config.languages.python;

nixpkgs-python = inputs.nixpkgs-python or (throw ''
To use languages.python.version, you need to add the following to your devenv.yaml:
devenvlib = import ../devenv-lib.nix { inherit pkgs config inputs lib; };

inputs:
nixpkgs-python:
url: github:cachix/nixpkgs-python
'');
nixpkgs-python = devenvlib.getInput {
name = "nixpkgs-python";
url = "github:cachix/nixpkgs-python";
attribute = "languages.python.version";
};

initVenvScript = pkgs.writeShellScript "init-venv.sh" ''
# Make sure any tools are not attempting to use the python interpreter from any
Expand Down
18 changes: 8 additions & 10 deletions src/modules/languages/ruby.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
let
cfg = config.languages.ruby;

nixpkgs-ruby = inputs.nixpkgs-ruby or (throw ''
To use languages.ruby.version or languages.ruby.versionFile, you need to add the following to your devenv.yaml:
inputs:
nixpkgs-ruby:
url: github:bobvanderlinden/nixpkgs-ruby
inputs:
nixpkgs:
follows: nixpkgs
'');
devenvlib = import ../devenv-lib.nix { inherit pkgs config inputs lib; };

nixpkgs-ruby = devenvlib.getInput {
name = "nixpkgs-ruby";
url = "github:bobvanderlinden/nixpkgs-ruby";
attribute = "languages.ruby.version or languages.ruby.versionFile";
follows = [ "nixpkgs" ];
};
in
{
options.languages.ruby = {
Expand Down
24 changes: 11 additions & 13 deletions src/modules/languages/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

let
cfg = config.languages.rust;
setup = ''
inputs:
fenix:
url: github:nix-community/fenix
inputs:
nixpkgs:
follows: nixpkgs
'';

error = dbg: "To use languages.rust.${dbg}, you need to add the following to your devenv.yaml:\n\n${setup}";
devenvlib = import ../devenv-lib.nix { inherit pkgs config inputs lib; };

fenix = devenvlib.getInput {
name = "fenix";
url = "github:nix-community/fenix";
attribute = "languages.rust.version";
follows = [ "nixpkgs" ];
};

in
{
imports = [
Expand Down Expand Up @@ -63,7 +63,7 @@ in

config = lib.mkMerge [
(lib.mkIf cfg.enable {
packages = (builtins.map (c: cfg.toolchain.${c} or (throw (error "toolchain.${c}"))) cfg.components)
packages = (builtins.map (c: cfg.toolchain.${c} or fenix) cfg.components)
++ lib.optional pkgs.stdenv.isDarwin pkgs.libiconv;

# enable compiler tooling by default to expose things like cc
Expand All @@ -87,9 +87,7 @@ in
})
(lib.mkIf (cfg.channel != "nixpkgs") (
let
err = error "channel";
fenix = inputs.fenix or (throw err);
rustPackages = fenix.packages.${pkgs.stdenv.system} or (throw err);
rustPackages = fenix.packages.${pkgs.stdenv.system};
in
{
languages.rust.toolchain =
Expand Down
2 changes: 1 addition & 1 deletion src/modules/latest-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.3
1.0

0 comments on commit 4d227ef

Please sign in to comment.