1{
2 pkgs,
3 config,
4 lib,
5 ...
6}:
7
8let
9 cfg = config.services.envfs;
10 mounts = {
11 "/usr/bin" = {
12 device = "none";
13 fsType = "envfs";
14 options = [
15 "bind-mount=/bin"
16 "fallback-path=${
17 pkgs.runCommand "fallback-path" { } (
18 ''
19 mkdir -p $out
20 ln -s ${config.environment.usrbinenv} $out/env
21 ln -s ${config.environment.binsh} $out/sh
22 ''
23 + cfg.extraFallbackPathCommands
24 )
25 }"
26 "nofail"
27 ];
28 };
29 # We need to bind-mount /bin to /usr/bin, because otherwise upgrading
30 # from envfs < 1.0.5 will cause having the old envs with no /bin bind mount.
31 # Systemd is smart enough to not mount /bin if it's already mounted.
32 "/bin" = {
33 device = "/usr/bin";
34 fsType = "none";
35 options = [
36 "bind"
37 "nofail"
38 ];
39 };
40 };
41in
42{
43 options = {
44 services.envfs = {
45 enable = lib.mkEnableOption "Envfs filesystem" // {
46 description = ''
47 Fuse filesystem that returns symlinks to executables based on the PATH
48 of the requesting process. This is useful to execute shebangs on NixOS
49 that assume hard coded locations in locations like /bin or /usr/bin
50 etc.
51 '';
52 };
53
54 package = lib.mkOption {
55 type = lib.types.package;
56 default = pkgs.envfs;
57 defaultText = lib.literalExpression "pkgs.envfs";
58 description = "Which package to use for the envfs.";
59 };
60
61 extraFallbackPathCommands = lib.mkOption {
62 type = lib.types.lines;
63 default = "";
64 example = "ln -s $''{pkgs.bash}/bin/bash $out/bash";
65 description = "Extra commands to run in the package that contains fallback executables in case not other executable is found";
66 };
67 };
68 };
69 config = lib.mkIf (cfg.enable) {
70 environment.systemPackages = [ cfg.package ];
71 # we also want these mounts in virtual machines.
72 fileSystems = if config.virtualisation ? qemu then lib.mkVMOverride mounts else mounts;
73
74 # We no longer need those when using envfs
75 system.activationScripts.usrbinenv = lib.mkForce "";
76 system.activationScripts.binsh = lib.mkForce "";
77 };
78}