Best way to mount partitions?

I have partitions on internal disks that I want to be mounted when I log in, because I use the data on them.
So I put that into my config file.
But this makes my system fragile, because if one of those partitions is not available, I can’t log in to a desktop session.
What’s the best way to handle this?
The automounting lines are under the comment #Automount

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Sane Scanner
     hardware.sane.enable = true; 
     hardware.sane.disabledDefaultBackends = [ ".*" ];

  # Bootloader.
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  networking.hostName = "nixos"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Enable networking
  networking.networkmanager.enable = true;

  # Set your time zone.
  time.timeZone = "Asia/Jerusalem";

  #local time
  time.hardwareClockInLocalTime = true;

  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";

  i18n.extraLocaleSettings = {
    LC_ADDRESS = "en_US.UTF-8";
    LC_IDENTIFICATION = "en_US.UTF-8";
    LC_MEASUREMENT = "en_US.UTF-8";
    LC_MONETARY = "en_US.UTF-8";
    LC_NAME = "en_US.UTF-8";
    LC_NUMERIC = "en_US.UTF-8";
    LC_PAPER = "en_US.UTF-8";
    LC_TELEPHONE = "en_US.UTF-8";
    LC_TIME = "en_US.UTF-8";
  };

  # Enable the X11 windowing system.
  services.xserver.enable = true;

  # Enable the KDE Plasma Desktop Environment.
  services.xserver.displayManager.sddm.enable = true;
  services.xserver.desktopManager.plasma5.enable = true;

  # Configure keymap in X11
  services.xserver = {
   xkb.layout = "us";
    xkb.variant = "";
  };

  # Enable CUPS to print documents.
  services.printing.enable = true;

#Brother printer
  services.printing.drivers = [
    pkgs.brlaser
    pkgs.brgenml1lpr
    pkgs.brgenml1cupswrapper
];

  # Enable sound with pipewire.
  sound.enable = true;
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    # If you want to use JACK applications, uncomment this
    #jack.enable = true;

    # use the example session manager (no others are packaged yet so this is enabled by default,
    # no need to redefine it in your config for now)
    #media-session.enable = true;
  };

  # Enable touchpad support (enabled default in most desktopManager).
  # services.xserver.libinput.enable = true;

  #NTFS
  boot.supportedFilesystems = [ "ntfs" ];

#Automount

  fileSystems."/run/media/shmuel/Timeshift" = {
       device = "/dev/disk/by-uuid/701FE3AE54D4CE16";
};

fileSystems."/run/media/shmuel/VM" = {
    device = "/dev/disk/by-uuid/f8c56958-b308-4a9c-a8e6-f2ba9a148860";
};

fileSystems."/run/media/shmuel/PersonalData" = {
    device = "/dev/disk/by-uuid/22EC446AEC4439F5";
};

#swap
    zramSwap.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.shmuel = {
    isNormalUser = true;
    description = "shmuel";
    extraGroups = [ "networkmanager" "wheel" ];
    packages = with pkgs; [
      firefox
      kate
    #  thunderbird
    ];
  };

#Shell
  users.users.shmuel.shell = pkgs.zsh;

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

#Automatic updates
  system.autoUpgrade.enable  = true;
  system.autoUpgrade.allowReboot  = false;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
     pkgs.xfce.mousepad
     pkgs.deja-dup
     pkgs. duplicity
     pkgs.ntfs3g
     pkgs.google-chrome
     pkgs.caffeine-ng
     pkgs.audacious
     pkgs.timeshift
     pkgs.gparted
     pkgs.conky
     pkgs.goldendict-ng
     pkgs.losslesscut-bin
     pkgs.vlc
     pkgs.smplayer
     pkgs.libreoffice
     pkgs.handbrake
     pkgs.media-downloader
     pkgs.inxi
     pkgs.kio-admin
     pkgs.konsave
     pkgs.simple-scan
     pkgs.git
     pkgs.gh
     pkgs.vim
     pkgs.wget
     pkgs.curl
     pkgs.galculator
     pkgs.oh-my-zsh
     pkgs.neofetch
     pkgs.lolcat
     pkgs.zsh-autosuggestions
     pkgs.zoom-us
     pkgs.libressl
     pkgs.hebcal
     pkgs.ocs-url
     pkgs.git-credential-manager

  ];

environment.plasma5.excludePackages = with pkgs.libsForQt5; [
  plasma-browser-integration
  oxygen
];

  virtualisation.virtualbox.host.enable = true;
  virtualisation.virtualbox.host.enableExtensionPack = true;

  # Some programs need SUID wrappers, can be configured further or are
  # started in user sessions.
  # programs.mtr.enable = true;
  #programs.zsh.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

 programs.zsh = {
   enable = true;
   autosuggestions.enable = true;
   shellInit = "neofetch|lolcat";
   
   shellAliases = {
    ll = "ls -l";
    fax = "brpcfax -o fax-number=025389272 /home/shmuel/Downloads/Fax/*";
    del = "rm /home/shmuel/Downloads/Fax/*";
    bild = "sudo nixos-rebuild switch && cp /etc/nixos/configuration.nix ~/Documents/config && cd ~/Documents/config && git add configuration.nix && git commit -m config && git push origin master";
    up = "sudo nix-channel --update && sudo nixos-rebuild switch && cd ~/Documents/config && git add configuration.nix && git commit -m config && git push origin master";
    config = "sudo nano /etc/nixos/configuration.nix";
  };
};
programs.zsh.ohMyZsh = {
  enable = true;
  plugins = [  ];
  theme = "agnoster";
};

# Prevent the new user dialog in zsh
system.userActivationScripts.zshrc = "touch .zshrc";
  
# List services that you want to enable:
      services.flatpak.enable = true;
      services.avahi.enable = true;

  # Enable the OpenSSH daemon.
    services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  # This value determines the NixOS release from which the default
  # settings for stateful data, like file locations and database versions
  # on your system were taken. It‘s perfectly fine and recommended to leave
  # this value at the release version of the first install of this system.
  # Before changing this value read the documentation for this option
  # (e.g. man configuration.nix or on https://nixos.org/nixos/options.html).
  system.stateVersion = "23.11"; # Did you read the comment?

}

# Set channel by running this command:
# sudo nix-channel --add https://channels.nixos.org/nixos-unstable
# sudo nix-channel --update
# sudo nixos-rebuild switch --upgrade

# Configure printer:
# ipp://192.168.150.170:631
# Brother MFC-L2710DW series, using brlaser v6

Systemd mount units have an option to be tolerant of failures and that can be straightforwardly expressed in Nix.

filesystems."mountpoint".options = ["nofail"];

https://search.nixos.org/options?channel=23.11&from=0&size=50&sort=relevance&type=packages&query=filesystems.<name>.options

https://www.freedesktop.org/software/systemd/man/latest/systemd.mount.html

(You may additionally mark any systemd units that are dependent on that path so that it skips startup if the mount didn’t occur.)

systemd.services."name".unitConfig.RequiresMountsFor = ["mountpoint"];

I am having trouble with this one.
I get

error: The option `filesystems' does not exist.
sudo nixos-rebuild switch
error:
       … while evaluating the attribute 'config'

         at /nix/store/c38j2a01ps7dbqnyqn4mbl6y8bm0byf0-nixos-24.05pre590113.1536926ef562/nixos/lib/modules.nix:322:9:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          323|         _module = checked (config._module);

       … while calling the 'seq' builtin

         at /nix/store/c38j2a01ps7dbqnyqn4mbl6y8bm0byf0-nixos-24.05pre590113.1536926ef562/nixos/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: The option `filesystems' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             mountpoint = {
               options = [
                 "nofail"
               ];
           ...
building Nix...
error:
       … while evaluating the attribute 'config'

         at /nix/store/c38j2a01ps7dbqnyqn4mbl6y8bm0byf0-nixos-24.05pre590113.1536926ef562/nixos/lib/modules.nix:322:9:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          323|         _module = checked (config._module);

       … while calling the 'seq' builtin

         at /nix/store/c38j2a01ps7dbqnyqn4mbl6y8bm0byf0-nixos-24.05pre590113.1536926ef562/nixos/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: The option `filesystems' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             mountpoint = {
               options = [
                 "nofail"
               ];
           ...
building the system configuration...
error:
       … while evaluating the attribute 'config.system.build.toplevel'

         at /nix/store/c38j2a01ps7dbqnyqn4mbl6y8bm0byf0-nixos-24.05pre590113.1536926ef562/nixos/lib/modules.nix:322:9:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          323|         _module = checked (config._module);

       … while calling the 'seq' builtin

         at /nix/store/c38j2a01ps7dbqnyqn4mbl6y8bm0byf0-nixos-24.05pre590113.1536926ef562/nixos/lib/modules.nix:322:18:

          321|         options = checked options;
          322|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          323|         _module = checked (config._module);

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: The option `filesystems' does not exist. Definition values:
       - In `/etc/nixos/configuration.nix':
           {
             mountpoint = {
               options = [
                 "nofail"
               ];
           ...

I should rather say that I don’t know how to fit this in with a snippet such as

fileSystems."/run/media/shmuel/PersonalData" = {
    device = "/dev/disk/by-uuid/22EC446AEC4439F5";
};

Like this:

fileSystems."/run/media/shmuel/PersonalData" = {
    device = "/dev/disk/by-uuid/22EC446AEC4439F5";
    options = ["nofail"];
};

(And the previous error was due to missing upper case ‘S’ in fileSystems....)

2 Likes