cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
4.27k stars 322 forks source link

Allow passing specialArgs in `imports` modules #967

Closed MrFoxPro closed 5 months ago

MrFoxPro commented 8 months ago

When importing devenv modules using imports = [...] it's impossible to specify args for imported modules.

domenkozar commented 8 months ago

Can you give an example what you'd like to achieve?

MrFoxPro commented 8 months ago

I'm using flake.parts and I want to split my devenv shell into several files and inject some global args, let's say app_config, edb, shell and so on:

{
  lib,
  inputs,
  ...
} @ rootArgs:
with builtins;
with lib; {
  perSystem = {
    system,
    self',
    pkgs,
    config,
    ...
  } @ systemArgs: let
    shell = systemArgs.config.devenv.shells.default;
    root_dir = shell.devenv.root;
    state_dir = shell.devenv.state;
    profile_dir = shell.devenv.profile;

    app_config = readTOML (readFile "${root_dir}/config.toml");

    edb = rec {
      dir = "${state_dir}/edgedb";
      port = "10706";
      socket = "${dir}/.s.EDGEDB.admin.${port}";
      args = "--dsn=${app_config.edgedb_dsn} --tls-security=insecure";
    };
  in {
    devenv.shells.default = {
      imports = [
        ./garage.nix
        ./edgedb.nix
        ./microservice1
      ];
    };
  };
}

In both nixos and flake.parts it's possible to inject args via _module.args = {} or specialArgs = {}.

MrFoxPro commented 5 months ago

I found that providing _module.args actually works!

{...} @ rootArgs: {
  imports = [./base.shell.nix];
  perSystem = {...} @ systemArgs: {
    devenv.shells = {
      default = {
        imports = [
          ({config, ...}: {
            _module.args = rec {
              inherit (config.devenv) root state profile;
              fromRoot = source: ''
                pushd ${root}
                  ${source}
                popd'';
              # ... your shared args
            };
          })
        ];
      };
    };
  };
}

base.shell.nix:

{
  ...
} @ rootArgs: {
  perSystem = {
    ...
  } @ systemArgs: {
    devenv.shells.default = {
      devenv,
      # viola! it appears here now: 
      root, 
      state,
      profile,
      fromRoot,
      ...
    } @ devenvArgs:{
       # your shell code
    };
  };
}