Skip to content

Repository files navigation

envar

This is a command-line tool that automatically switches values of environment variables based on the current directory path.

  1. Place the envar binary
  2. Install the shell hook
  3. Write the configuration file

Place the envar binary

Run go build and place the binary file in your PATH.

Install the shell hook

Add this line to your .bashrc:

eval "$(envar hook)"

and add this line to your .bash_logout:

envar hook logout $$

Write the configuration file

The configuration file uses YAML. It is located at $CONFIG_DIR/envar/vars.yaml and $CONFIG_DIR/envar/execs.yaml. $CONFIG_DIR is the value returned by os.UserConfigDir().

vars.yaml is used to define environment variable values. For example:

FOO_VAR:
  path/to/dir: foo-value-1
  other/path: foo-value-2
  other/path/never: foo-value-3
  # This is a comment line
  ~/projects: foo-value-4
  "spacial dir/path": foo-value-5
BAR_VAR:
  another/dir: bar-value-1

The directory path/to/dir and its subdirectories are associated with foo-value-1. The directory other/path and its subdirectories are associated with foo-value-2. foo-value-3 is never used because other/path/never is a subdirectory of other/path. The fourth line is a comment and is ignored. The fifth line demonstrates the use of ~. The sixth line shows an example of using double quotes.

You can unset a variable by specifying a null value:

FOO_VAR:
  path/to/dir:
# or
  path/to/dir: null

When no matching path prefix is found for a variable, it is unset.

You can compute values using a command, which is useful when you don't want to store secrets directly in the configuration file. For example, using the gh CLI to get a GitHub authentication token, you must prepare execs.yaml first like this:

gh: gh auth token --user %s

The key gh is a config variable that is referenced in vars.yaml later, and the value is a command template. The placeholder %s is replaced by the argument specified in vars.yaml.

Then, you can use it in vars.yaml like this:

GH_TOKEN:
  path/to/dir:
    gh: foo
  other/path:
    gh: bar

Placeholders can be placed multiple times in a command template, for example when execs.yaml is like this:

echo: bash -c 'echo %s and %s'

You can use it in vars.yaml like this:

ECHO_VAR:
  some/dir:
    echo: [ John, Alice ]

Note that no escaping is performed for the arguments.

Using with Nix's Home Manager

A Nix module for Home Manager is provided. You can write a Home Manager configuration like this:

modules = [
  envar.homeModules.default
  {
    programs.envar = {
      enable = true;
      enableBashIntegration = true;
      settings = {
        vars = {
          FOO_VAR = [
            { path = "path/to/dir"; value = "foo-value-1"; }
          ];
        };
        execs = {
          gh = "gh auth token --user %s";
        };
      };
    };
  };
];

You will find a full example at homeConfigurations.test in the flake.nix file.

Flake module options

See doc/options.md for the list of available options.

License

Nix source codes are licensed under the Apache-2.0 and Go source codes and others are licensed under the GPL-3.0-Only.

About

This is a command-line tool that automatically switches values of environment variables based on the current directory path.

Topics

Resources

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages