0

Basically, I want to pull my config.programs.config.alias entries into separate .bash files and read them dynamically when building the configuration. A representative subset of the current configuration:

{
  programs.git = {
    config = {
      alias = {
        aliases = "!git config --get-regexp '^alias\.' | cut --delimiter=. --fields 2-";
        git = "!git";
        st = "status";
      };
    };
  };
}

All of the !git entries would be better off as separate shell scripts. That way I can lint, format, and run them to verify that they work before integrating them in the Git aliases configuration.

1 Answer 1

0

Annotated version of the solution so far:

{
  config.git.config.alias =
    (
      lib.attrsets.mergeAttrsList ( # Change from a list of attribute sets to a single attribute set
        map (
          path: {
            # Create a [filename without extension as alias name] to [alias value] attribute set
            "${lib.removeSuffix ".bash" (baseNameOf path)}" =
              "!\"" # `!`denotes that this alias runs a command rather than a Git subcommand; quote to simplify escaping
              + builtins.replaceStrings ["\n"] ["; "] ( # Change from readable multi-line scripts to a single line
                lib.escape ["\"" "\\"] ( # Escape backslash and double quotes to fit Git configuration language
                  lib.removeSuffix "\n" ( # Remove newline at EOF
                    builtins.readFile path
                  )
                )
              )
              + "\"";
          }
        ) (
          lib.filesystem.listFilesRecursive ./includes/git-aliases
        )
      )
    )
    // {
      st = "status";
    };
}

It seems to work for all my aliases, including those with single and double quotes, backslashes, and newlines. Hopefully someone else comes up with something simpler, but this'll do for now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .