Skip to content

Instantly share code, notes, and snippets.

@cole-h

cole-h/help.md Secret

Last active March 1, 2020 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cole-h/523a5a94f636f2c6676ee9fd1e50991b to your computer and use it in GitHub Desktop.
Save cole-h/523a5a94f636f2c6676ee9fd1e50991b to your computer and use it in GitHub Desktop.

Using what I've learned here, PR is up! nix-community/home-manager#1063


If I have an attrset with fixed children (nullOr), but the name can be anything, like the following:

function /* or func, or fn, anything really */ = {
  body = "echo hi"; # nullOr lines
  description = "echoes hi"; # nullOr str
  on_event = "some_event"; # nullOr str
  # not pictured: wraps = null; # nullOr str
};

how would I translate that into the following (a bit pseudocode-y):

{
  name = "functions/${function}";
  value = {
    "text" = ''
      function ${function} --description="${function.description}" --on-event "${function.on_event}"
        ${function.body}
      end
    '';
  };
}

?

Basically, I am trying to add the ability to specify complex functions to home-manager's fish module. Its original implementation (found here) is too naive and doesn't support additional flags like above. However, I don't know how I would get from A to B, which functions to use, etc.


The goal is to go from either

func = "echo hi";

or

func = {
  body = "echo hi";
  on_event = "event";
};

to

function ${name} # --on-event=event for the second one
  ${body}
end
@d-goldin
Copy link

d-goldin commented Mar 1, 2020

Maybe something like the following is sufficient;

let
  lib = (import <nixpkgs> { }).lib;
  fishFn = name: def:
    let
      attrArgs = attrs:
        lib.concatStringsSep " "
        (lib.mapAttrsToList (name: val: ''--${name}="${val}"'') attrs);
    in {
      name = "functions/${name}";
      value = {
        text = ''
          function ${name} ${attrArgs def}
          ${def.body}
          end
        '';
      };
    };

in fishFn "function1" {
  body = "echo hi";
  description = "echoes hi";
  on_event = "some_event";
}

You can try it out like so, assuming above contents are in default.nix

$ nix-instantiate --strict --eval default.nix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment