2018-07-14 05:25:28 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.firejail;
|
|
|
|
|
2019-10-05 16:32:22 +00:00
|
|
|
wrappedBins = pkgs.runCommand "firejail-wrapped-binaries"
|
|
|
|
{ preferLocalBuild = true;
|
|
|
|
allowSubstitutes = false;
|
2022-11-30 17:45:16 +00:00
|
|
|
# take precedence over non-firejailed versions
|
|
|
|
meta.priority = -1;
|
2019-10-05 16:32:22 +00:00
|
|
|
}
|
|
|
|
''
|
2018-07-14 05:25:28 +00:00
|
|
|
mkdir -p $out/bin
|
2022-11-30 17:45:16 +00:00
|
|
|
mkdir -p $out/share/applications
|
2020-11-14 12:00:00 +00:00
|
|
|
${lib.concatStringsSep "\n" (lib.mapAttrsToList (command: value:
|
|
|
|
let
|
|
|
|
opts = if builtins.isAttrs value
|
|
|
|
then value
|
2022-11-30 17:45:16 +00:00
|
|
|
else { executable = value; desktop = null; profile = null; extraArgs = []; };
|
2020-11-14 12:00:00 +00:00
|
|
|
args = lib.escapeShellArgs (
|
2022-03-01 11:58:04 +00:00
|
|
|
opts.extraArgs
|
2024-04-17 11:37:58 +00:00
|
|
|
++ (lib.optional (opts.profile != null) "--profile=${builtins.toString opts.profile}")
|
2022-11-30 17:45:16 +00:00
|
|
|
);
|
2020-11-14 12:00:00 +00:00
|
|
|
in
|
|
|
|
''
|
2019-10-05 16:32:22 +00:00
|
|
|
cat <<_EOF >$out/bin/${command}
|
|
|
|
#! ${pkgs.runtimeShell} -e
|
2024-04-17 11:37:58 +00:00
|
|
|
exec /run/wrappers/bin/firejail ${args} -- ${builtins.toString opts.executable} "\$@"
|
2019-10-05 16:32:22 +00:00
|
|
|
_EOF
|
|
|
|
chmod 0755 $out/bin/${command}
|
2022-11-30 17:45:16 +00:00
|
|
|
|
|
|
|
${lib.optionalString (opts.desktop != null) ''
|
|
|
|
substitute ${opts.desktop} $out/share/applications/$(basename ${opts.desktop}) \
|
|
|
|
--replace ${opts.executable} $out/bin/${command}
|
|
|
|
''}
|
2018-07-14 05:25:28 +00:00
|
|
|
'') cfg.wrappedBinaries)}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.programs.firejail = {
|
2024-04-17 11:37:58 +00:00
|
|
|
enable = lib.mkEnableOption "firejail, a sandboxing tool for Linux";
|
2018-07-14 05:25:28 +00:00
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
wrappedBinaries = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.either lib.types.path (lib.types.submodule {
|
2020-11-14 12:00:00 +00:00
|
|
|
options = {
|
2024-04-17 11:37:58 +00:00
|
|
|
executable = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2024-04-13 12:54:15 +00:00
|
|
|
description = "Executable to run sandboxed";
|
2024-04-17 11:37:58 +00:00
|
|
|
example = lib.literalExpression ''"''${lib.getBin pkgs.firefox}/bin/firefox"'';
|
2020-11-14 12:00:00 +00:00
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
desktop = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
2022-11-30 17:45:16 +00:00
|
|
|
default = null;
|
2024-04-13 12:54:15 +00:00
|
|
|
description = ".desktop file to modify. Only necessary if it uses the absolute path to the executable.";
|
2024-04-17 11:37:58 +00:00
|
|
|
example = lib.literalExpression ''"''${pkgs.firefox}/share/applications/firefox.desktop"'';
|
2022-11-30 17:45:16 +00:00
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
profile = lib.mkOption {
|
|
|
|
type = lib.types.nullOr lib.types.path;
|
2020-11-14 12:00:00 +00:00
|
|
|
default = null;
|
2024-04-13 12:54:15 +00:00
|
|
|
description = "Profile to use";
|
2024-04-17 11:37:58 +00:00
|
|
|
example = lib.literalExpression ''"''${pkgs.firejail}/etc/firejail/firefox.profile"'';
|
2020-11-14 12:00:00 +00:00
|
|
|
};
|
2024-04-17 11:37:58 +00:00
|
|
|
extraArgs = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2020-11-14 12:00:00 +00:00
|
|
|
default = [];
|
2024-04-13 12:54:15 +00:00
|
|
|
description = "Extra arguments to pass to firejail";
|
2020-11-14 12:00:00 +00:00
|
|
|
example = [ "--private=~/.firejail_home" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}));
|
2018-07-14 05:25:28 +00:00
|
|
|
default = {};
|
2024-04-17 11:37:58 +00:00
|
|
|
example = lib.literalExpression ''
|
2020-02-29 17:55:53 +00:00
|
|
|
{
|
2020-11-14 12:00:00 +00:00
|
|
|
firefox = {
|
|
|
|
executable = "''${lib.getBin pkgs.firefox}/bin/firefox";
|
|
|
|
profile = "''${pkgs.firejail}/etc/firejail/firefox.profile";
|
|
|
|
};
|
|
|
|
mpv = {
|
|
|
|
executable = "''${lib.getBin pkgs.mpv}/bin/mpv";
|
|
|
|
profile = "''${pkgs.firejail}/etc/firejail/mpv.profile";
|
|
|
|
};
|
2020-02-29 17:55:53 +00:00
|
|
|
}
|
|
|
|
'';
|
2024-04-13 12:54:15 +00:00
|
|
|
description = ''
|
2018-07-14 05:25:28 +00:00
|
|
|
Wrap the binaries in firejail and place them in the global path.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2021-09-12 16:53:48 +00:00
|
|
|
security.wrappers.firejail =
|
|
|
|
{ setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
group = "root";
|
|
|
|
source = "${lib.getBin pkgs.firejail}/bin/firejail";
|
|
|
|
};
|
2018-07-14 05:25:28 +00:00
|
|
|
|
2020-02-02 18:21:26 +00:00
|
|
|
environment.systemPackages = [ pkgs.firejail ] ++ [ wrappedBins ];
|
2018-07-14 05:25:28 +00:00
|
|
|
};
|
|
|
|
|
2024-04-17 11:37:58 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ peterhoeg ];
|
2018-07-14 05:25:28 +00:00
|
|
|
}
|