How to auto-mount with sshfs as a normal user?

I ran ssh-copy-id so I can ssh to rajas@gaming-computer without entering the password. I can mount it with this command:

sshfs gaming-computer:/ ~/gaming-computer

I tried using the home manager sftpman option like this:

{ pkgs, ... }:

{
  programs.sftpman = {
    enable = true;
    mounts = {
      gaming-computer = {
        host = "gaming-computer";
        user = "rajas";
        mountOptions = [
          "~/gaming-computer"
        ];
        mountPoint = "/";
      };
    };
    defaultSshKey = "~/.ssh/id_ed25519";
    package = pkgs.sshfs;
  };
}

home-manager switch exits successfully but my folder doesn’t get mounted.

I looked at Mount sshf as a user using home-manager but it’s very complicated and I don’t think it has to be that complicated.

Also, any suggestions to having a faster (faster file reads) way of mounting the external folder? It is pretty slow using the sshfs command.

auto-mount can mean a few different things here. using nixos’ fileSystems.<mount-point> options (not home manager) can probably get what you want though.

fileSystems."/home/rajas/gaming-computer" = {
  device = "rajas@gaming-computer:/";
  fsType = "fuse.sshfs";
  options = [
    "identityfile=/home/rajas/.ssh/id_ed25519"
    "idmap=user"
    "x-systemd.automount"  #< mount the filesystem automatically on first access
    "allow_other"  #< don't restrict access to only the user which `mount`s it (because that's probably systemd who mounts it, not you)
    "user"  #< allow manual `mount`ing, as ordinary user.
  ];
};
boot.supportedFilesystems."fuse.sshfs" = true;

you might also want "noauto" (to not block system boot on this mount), "noCheck", "_netdev"/"x-systemd.requires=network-online.target" "x-systemd.after=network-online.target", "default_permissions", and "uid=####" "gid=###" (get from id command) if you use this method.

i haven’t tried this with a password-protected ssh key though – not sure if that works.

2 Likes

I got it to work after a few changes:

{ ... }:

{
  fileSystems."/mnt/gaming-computer" = {
    device = "rajas@gaming-computer:/";
    fsType = "fuse.sshfs";
    options = [
      "identityfile=/root/.ssh/id_ed25519"
      "idmap=user"
      "x-systemd.automount" #< mount the filesystem automatically on first access
      "allow_other" #< don't restrict access to only the user which `mount`s it (because that's probably systemd who mounts it, not you)
      "user" #< allow manual `mount`ing, as ordinary user.
      "_netdev"
    ];
  };
  boot.supportedFilesystems."fuse.sshfs" = true;
}
  • Trying to mount it in my home folder resulted in me not being able to access it without sudo for some reason
  • My ssh key is not password protected
2 Likes