Aliases not working for fish shell + sudo

I have the following setup:

home-configuration.scm

(services
   (append (list (service home-bash-service-type
                          (home-bash-configuration
     (aliases '(("sudo" . "sudo ") 
      ...
      ("vim" . "nvim")
   (service home-fish-service-type
     (home-fish-configuration
       (aliases '(("sudo" . "sudo ") 
        ...
         ("vim" . "nvim")

This causes vim and sudo vim to work for bash and vim for fish including when logging as root user via su, but unfortunately sudo vim does not work using fish.

> sudo vim
sudo: vim: command not found

What could be the cause?

You should diff the various environments with those aliases I posted on the other thread.

  • bash: env and sudo env
  • fish: env and sudo env
  • bash and fish

It’s likely you have something in your .bash_profile (or .profile) which isn’t being set up for fish.

Also, your window manager will set up a profile when it loads. Sometimes you need to login & logout. The Guix home container should show updates.

This is all a bit confusing when setting it up for yourself in Arch (from scratch), NixOS or Guix.

I haven’t used fish, so I’m not 100% sure.

Again, check sudo env | grep -e ‘^PATH’ | tr ‘:’ ‘\n’ from within fish. Or use alias. Or whatever command works in fish

Sudo has some environment differences. So it can’t find vim in PATH

Checking the envs I’ve noticed that all the PATHs are the same.
On top of that I’ve (re-)discovered that all aliases are not working.

> df

Filesystem                    Size  Used Avail Use% Mounted on
none                           18G     0   18G   0% /dev
...

> sudo df
Filesystem             1K-blocks      Used  Available Use% Mounted on
none                    18250332         0   18250332   0% /dev
...

> bash
$ df

Filesystem                    Size  Used Avail Use% Mounted on
none                           18G     0   18G   0% /dev
...

$ sudo df

Filesystem                    Size  Used Avail Use% Mounted on
none                           18G     0   18G   0% /dev
...

home-configuration.scm

services
   (append (list (service home-bash-service-type
                          (home-bash-configuration
     (aliases '(("sudo" . "sudo ") 
      ...
      ("df" . "df -h")
   (service home-fish-service-type
     (home-fish-configuration
       (aliases '(("sudo" . "sudo ") 
        ...
         ("df" . "df -h")

This seems to be a fish shell issue.
The bash trick doesn’t work, so one has to write a fish alias function, remove the sudo alias for the home-fish-service-type and since there’s no functions option for home-fish-configuration, I put the alias function in the config.fish file, which loads at startup.

~/dotfiles/home/folaht/main-computer.scm

(define-module (config home folaht main-computer)
  #:use-module (gnu home)
  ...
  #:use-module (gnu packages shells)
  #:use-module (config home .services shells)
  ...)

(home-environment
  ...
  (services
   (append (list home-fish-service-default
                 ...)
           %base-home-services)))

~/dotfiles/home/.services/shells.scm

(define shell-aliases
  '(("df" . "df -h") 
    ...
    (".." . "cd ../")))

(define bash-aliases (append '(("sudo" . "sudo ")) shell-aliases))

(define home-bash-service-default
  (service home-bash-service-type
    (home-bash-configuration
      ...
      (aliases bash-aliases)
      ...)))

(define home-fish-service-default
  (service home-fish-service-type
           (home-fish-configuration 
             (config (list (local-file "../.config/fish/functions/sudo.fish")))
             (aliases shell-aliases)))))

~dotfiles/home/.config/fish/functions/sudo.fish

function sudo
    if functions -q -- "$argv[1]"
        set cmdline (
            for arg in $argv
                printf "\"%s\" " $arg
            end
        )
        set -x function_src (string join "\n" (string escape --style=var (functions "$argv[1]")))
        set argv fish -c 'string unescape --style=var (string split "\n" $function_src) | source; '$cmdline
        command sudo -E $argv
    else
        command sudo $argv
    end
end