summaryrefslogtreecommitdiff
path: root/modules/envfs.nix
blob: 1463dce8f094cd90681d5f7efc33cca6be0dffd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{ pkgs, config, lib, ... }:

let
  cfg = config.services.envfs;
  mounts = {
    "/usr/bin" = {
      device = "none";
      fsType = "envfs";
      options = [
        "fallback-path=${pkgs.symlinkJoin {
          name = "fallback-path";
          inherit (cfg) paths;
        }}"
      ];
    };
    "/bin" = {
      device = "/usr/bin";
      fsType = "none";
      options = [ "bind" "nofail" ];
    };
  };
in {
  disabledModules = [ "tasks/filesystems/envfs.nix" ];

  options = {
    services.envfs = {
      enable = lib.mkEnableOption (lib.mdDoc "Envfs filesystem") // {
        default = true;
        description = lib.mdDoc ''
          Fuse filesystem that returns symlinks to executables based on the PATH
          of the requesting process. This is useful to execute shebangs on NixOS
          that assume hard coded locations in locations like /bin or /usr/bin
          etc.
        '';
      };

      package = lib.mkOption {
        type = lib.types.package;
        default = pkgs.envfs;
        defaultText = lib.literalExpression "pkgs.envfs";
        description = lib.mdDoc "Which package to use for the envfs.";
      };

      paths = lib.mkOption {
        type = lib.types.listOf lib.types.package;
        default = [
          (pkgs.runCommand "fallback-path-environment" {} ''
            mkdir -p $out
            ln -s ${config.environment.usrbinenv} $out/env
            ln -s ${config.environment.binsh} $out/sh
          '')
        ];
        description = lib.mdDoc "Extra packages to join into collection of fallback executables in case not other executable is found";
      };
    };
  };

  config = lib.mkIf (cfg.enable) {
    environment.systemPackages = [ cfg.package ];
    # we also want these mounts in virtual machines.
    fileSystems = if config.virtualisation ? qemu then lib.mkVMOverride mounts else mounts;

    # We no longer need those when using envfs
    system.activationScripts.usrbinenv = lib.mkForce "";
    system.activationScripts.binsh = lib.mkForce "";
  };
}