Automatically Loading Fish Functions in NixOS

I’m looking for some guidance on how to make a Fish function load automatically when I open my terminal. Currently, I have to manually invoke the function each time, which can be quite tedious.

Here’s the relevant part of my configuration file:

    fish = {
      enable = true;
      shellAliases = {
        ll = "eza -l";
        la = "eza -a";
        ls = "eza";
        tree = "eza --tree";
        fetch = "clear && fastfetch";
        ".." = "cd ..";
        nfu = "cd /etc/nixos/ || exit && nix flake update";
        nrs = "cd /etc/nixos/ || exit && doas nixos-rebuild switch --flake .";
        nso = "cd /etc/nixos/ || exit && nix store optimise";
        ncg = "cd /etc/nixos/ || exit && doas nix-collect-garbage --delete-older-than 3d";
        hms = "cd /etc/nixos/ || exit && home-manager switch --flake .";
        #unx = "cd /etc/nixos/ || exit && nix flake update && doas nixos-rebuild switch --flake . && home-manager switch --flake . && nix store optimise && doas nix-collect-garbage --delete-older-than 3d";
        ttm = "tt -quotes en -theme catppuccin-mocha";
        cat = "bat";
        cd = "z";
        syn = "rsync -a /etc/nixos /home/user/Documents/";
        bak = "cd /home/user/Documents/nixos/ || exit && git add . && git commit -m \"Backup commit\" && git push codeberg master";
      };
      functions = {
          unx = ''
            function unx
                set -l skip_flake_update 0
                set -l skip_nixos_rebuild 0
                set -l skip_home_manager 0
                set -l skip_optimize 0
                set -l skip_garbage 0

                function _unx_show_help --description "Show usage for unx"
                    echo "Usage: unx [-s|--skip <commands>] [options]"
                    echo ""
                    echo "Options:"
                    echo "  -s, --skip <commands>   Skip the specified commands."
                    echo "    Commands:"
                    echo "      f  Skip flake update"
                    echo "      r  Skip NixOS rebuild"
                    echo "      h  Skip home manager"
                    echo "      o  Skip optimization"
                    echo "      g  Skip garbage collection"
                    echo "  -h, --help               Show this help message."
                    echo ""
                    echo "If no options are provided, all commands will be executed."
                end

                set -l skip_mode 0
                set -l i 1

                while test $i -le (count $argv)
                    set -l arg $argv[$i]
                    switch $arg
                        case '-h' '--help'
                            _unx_show_help
                            return
                        case '-s' '--skip'
                            set skip_mode 1
                            set i (math $i + 1)
                            while test $i -le (count $argv)
                                switch $argv[$i]
                                    case 'f'
                                        set skip_flake_update 1
                                    case 'r'
                                        set skip_nixos_rebuild 1
                                    case 'h'
                                        set skip_home_manager 1
                                    case 'o'
                                        set skip_optimize 1
                                    case 'g'
                                        set skip_garbage 1
                                    case '*'
                                        _unx_show_help
                                        return 1
                                end
                                set i (math $i + 1)
                            end
                            break
                        case '*'
                            _unx_show_help
                            return 1
                    end
                    set i (math $i + 1)
                end

                cd /etc/nixos/ || return

                if test $skip_flake_update -eq 0
                    nix flake update
                end
                if test $skip_nixos_rebuild -eq 0
                    doas nixos-rebuild switch --flake .
                end
                if test $skip_home_manager -eq 0
                    home-manager switch --flake .
                end
                if test $skip_optimize -eq 0
                    nix store optimise
                end
                if test $skip_garbage -eq 0
                    doas nix-collect-garbage --delete-older-than 3d
                end
            end
          '';
      };
      interactiveShellInit = ''
        set fish_greeting # Disable greeting
      '';
    };

The function I want to load automatically is unx. Does anyone know how to configure this in my NixOS setup so that it loads every time I start a new terminal session? I want it to basically work as an alias if possible, where it is already saved so no need to type it once. Realize it’s not loaded because it didn’t do anything, and do it again for it to actually run.

Any help would be greatly appreciated!

Thank you!

https://search.nixos.org/options?channel=unstable&show=programs.fish.shellInit&from=0&size=30&sort=relevance&type=packages&query=fish

This looks like the home-manager module. Is it? If so: what is the contents of ~/.config/fish/functions. Does it have a unx.fish file in it? Wait… why is the function creating a function? Lose the outer wrapping function unx ... end. It does nothing.

ASIDE: argparse - parse options passed to a fish script or function — fish-shell 4.0.2 documentation you can simplify your argument handling with a builtin, if you so wish.

1 Like

You have to run it twice because the structure for defining a function is

functions = {
    name = body;
};

You set the body of your function to be

function name
    body
end

so the resulting function it generates is

function name
    function name
        body
    end
end

Remove the extra function unx / end in the body of the function and it will work.

also, your aliases are a bit weird. you don’t need the one for .., fish automatically will change into a directory if it’s given as a command. and the rest only need to be:

        nfu = "nix flake update --flake /etc/nixos";
        nrs = "doas nixos-rebuild switch --flake /etc/nixos";
        nso = "nix store optimise";
        ncg = "doas nix-collect-garbage --delete-older-than 3d";
        hms = "home-manager switch --flake /etc/nixos";

and in general

cd /somewhere/ || exit && whatever

is the same as

cd /somewhere/ && whatever

but in those cases none of the commands require being in that directory.

1 Like