2024-09-04 17:03:21 +09:00
|
|
|
{
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
config,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
types
|
|
|
|
mkOption
|
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
2024-11-18 02:49:48 +09:00
|
|
|
mkDefault
|
2024-09-04 17:03:21 +09:00
|
|
|
mapAttrs
|
|
|
|
mapAttrsToList
|
2024-11-18 02:49:48 +09:00
|
|
|
mergeAttrsList
|
|
|
|
imap1
|
|
|
|
foldr
|
2024-09-04 17:03:21 +09:00
|
|
|
foldlAttrs
|
|
|
|
optional
|
2024-11-06 04:25:15 +09:00
|
|
|
optionals
|
2024-09-04 17:03:21 +09:00
|
|
|
;
|
|
|
|
|
|
|
|
cfg = config.environment.fortify;
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
environment.fortify = {
|
|
|
|
enable = mkEnableOption "fortify";
|
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.callPackage ./package.nix { };
|
|
|
|
description = "Package providing fortify.";
|
|
|
|
};
|
|
|
|
|
|
|
|
users = mkOption {
|
|
|
|
type =
|
|
|
|
let
|
|
|
|
inherit (types) attrsOf ints;
|
|
|
|
in
|
|
|
|
attrsOf (ints.between 0 99);
|
|
|
|
description = ''
|
|
|
|
Users allowed to spawn fortify apps, as well as their fortify ID value.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
apps = mkOption {
|
2024-09-04 17:03:21 +09:00
|
|
|
type =
|
|
|
|
let
|
|
|
|
inherit (types)
|
|
|
|
str
|
|
|
|
enum
|
|
|
|
bool
|
|
|
|
package
|
|
|
|
anything
|
|
|
|
submodule
|
|
|
|
listOf
|
|
|
|
attrsOf
|
|
|
|
nullOr
|
2024-11-06 04:25:15 +09:00
|
|
|
functionTo
|
2024-09-04 17:03:21 +09:00
|
|
|
;
|
|
|
|
in
|
2024-11-18 02:49:48 +09:00
|
|
|
listOf (submodule {
|
2024-09-04 17:03:21 +09:00
|
|
|
options = {
|
2024-11-18 02:49:48 +09:00
|
|
|
name = mkOption {
|
|
|
|
type = str;
|
|
|
|
description = ''
|
|
|
|
App name, typically command.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
id = mkOption {
|
|
|
|
type = nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Freedesktop application ID.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-04 17:03:21 +09:00
|
|
|
packages = mkOption {
|
|
|
|
type = listOf package;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
List of extra packages to install via home-manager.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = anything;
|
|
|
|
default = { };
|
|
|
|
description = "Extra home-manager configuration.";
|
|
|
|
};
|
2024-11-06 14:09:41 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
script = mkOption {
|
|
|
|
type = nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Application launch script.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-04 17:03:21 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
command = mkOption {
|
|
|
|
type = nullOr str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Command to run as the target user.
|
|
|
|
Setting this to null will default command to wrapper name.
|
|
|
|
Has no effect when script is set.
|
|
|
|
'';
|
|
|
|
};
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
groups = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
List of groups to inherit from the privileged user.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-09 04:37:42 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
dbus = {
|
|
|
|
session = mkOption {
|
|
|
|
type = nullOr (functionTo anything);
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
D-Bus session bus custom configuration.
|
|
|
|
Setting this to null will enable built-in defaults.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-09 21:26:14 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
system = mkOption {
|
|
|
|
type = nullOr anything;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
D-Bus system bus custom configuration.
|
|
|
|
Setting this to null will disable the system bus proxy.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2024-09-09 04:37:42 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
env = mkOption {
|
|
|
|
type = nullOr (attrsOf str);
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Environment variables to set for the initial process in the sandbox.
|
|
|
|
'';
|
|
|
|
};
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
nix = mkEnableOption ''
|
|
|
|
Whether to allow nix daemon connections from within sandbox.
|
|
|
|
'';
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
userns = mkEnableOption ''
|
|
|
|
Whether to allow userns within sandbox.
|
|
|
|
'';
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
mapRealUid = mkEnableOption ''
|
|
|
|
Whether to map to fortify's real UID within the sandbox.
|
|
|
|
'';
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
net =
|
|
|
|
mkEnableOption ''
|
|
|
|
Whether to allow network access within sandbox.
|
|
|
|
''
|
|
|
|
// {
|
|
|
|
default = true;
|
|
|
|
};
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
gpu = mkOption {
|
|
|
|
type = nullOr bool;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Target process GPU and driver access.
|
|
|
|
Setting this to null will enable GPU whenever X or Wayland is enabled.
|
|
|
|
'';
|
|
|
|
};
|
2024-11-06 04:25:15 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
dev = mkEnableOption ''
|
|
|
|
Whether to allow access to all devices within sandbox.
|
|
|
|
'';
|
2024-09-09 04:37:42 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
extraPaths = mkOption {
|
|
|
|
type = listOf anything;
|
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
Extra paths to make available inside the sandbox.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-08 02:45:00 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
capability = {
|
|
|
|
wayland = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to share the Wayland socket.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-08 02:45:00 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
x11 = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to share the X11 socket and allow connection.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-08 02:45:00 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
dbus = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to proxy D-Bus.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-04 17:03:21 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
pulse = mkOption {
|
|
|
|
type = bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to share the PulseAudio socket and cookie.
|
|
|
|
'';
|
|
|
|
};
|
2024-09-04 17:03:21 +09:00
|
|
|
};
|
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
share = mkOption {
|
|
|
|
type = nullOr package;
|
|
|
|
default = null;
|
2024-09-04 17:03:21 +09:00
|
|
|
description = ''
|
2024-11-18 02:49:48 +09:00
|
|
|
Package containing share files.
|
|
|
|
Setting this to null will default package name to wrapper name.
|
2024-09-04 17:03:21 +09:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2024-11-18 02:49:48 +09:00
|
|
|
default = [ ];
|
|
|
|
description = "Applications managed by fortify.";
|
2024-09-04 17:03:21 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
stateDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
The path to persistent storage where per-user state should be stored.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2024-11-18 02:49:48 +09:00
|
|
|
security.wrappers.fsu = {
|
|
|
|
source = "${cfg.package}/libexec/fsu";
|
|
|
|
setuid = true;
|
|
|
|
owner = "root";
|
|
|
|
setgid = true;
|
|
|
|
group = "root";
|
|
|
|
};
|
2024-09-04 17:03:21 +09:00
|
|
|
|
2024-11-18 02:49:48 +09:00
|
|
|
environment.etc = {
|
|
|
|
fsurc = {
|
|
|
|
mode = "0400";
|
|
|
|
text = foldlAttrs (
|
|
|
|
acc: username: fid:
|
|
|
|
"${toString config.users.users.${username}.uid} ${toString fid}\n" + acc
|
|
|
|
) "" cfg.users;
|
|
|
|
};
|
|
|
|
|
|
|
|
userdb.source = pkgs.runCommand "generate-userdb" { } ''
|
|
|
|
${cfg.package}/libexec/fuserdb -o $out ${
|
|
|
|
foldlAttrs (
|
|
|
|
acc: username: fid:
|
|
|
|
acc + " ${username}:${toString fid}"
|
|
|
|
) "-s /run/current-system/sw/bin/nologin -d ${cfg.stateDir}" cfg.users
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
services.userdbd.enable = mkDefault true;
|
|
|
|
|
|
|
|
home-manager =
|
|
|
|
let
|
|
|
|
privPackages = mapAttrs (username: fid: {
|
|
|
|
home.packages =
|
|
|
|
let
|
|
|
|
# aid 0 is reserved
|
|
|
|
wrappers = imap1 (
|
|
|
|
aid: app:
|
2024-09-04 17:03:21 +09:00
|
|
|
let
|
2024-11-06 04:25:15 +09:00
|
|
|
extendDBusDefault = id: ext: {
|
|
|
|
filter = true;
|
|
|
|
|
|
|
|
talk = [ "org.freedesktop.Notifications" ] ++ ext.talk;
|
|
|
|
own =
|
2024-11-18 02:49:48 +09:00
|
|
|
(optionals (app.id != null) [
|
2024-11-06 04:25:15 +09:00
|
|
|
"${id}.*"
|
|
|
|
"org.mpris.MediaPlayer2.${id}.*"
|
|
|
|
])
|
|
|
|
++ ext.own;
|
2024-11-10 22:24:17 +09:00
|
|
|
|
|
|
|
inherit (ext) call broadcast;
|
2024-11-06 04:25:15 +09:00
|
|
|
};
|
2024-09-09 04:37:42 +09:00
|
|
|
dbusConfig =
|
2024-11-06 04:25:15 +09:00
|
|
|
let
|
|
|
|
default = {
|
|
|
|
talk = [ ];
|
|
|
|
own = [ ];
|
|
|
|
call = { };
|
|
|
|
broadcast = { };
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
session_bus =
|
2024-11-18 02:49:48 +09:00
|
|
|
if app.dbus.session != null then
|
|
|
|
(app.dbus.session (extendDBusDefault app.id))
|
2024-11-06 04:25:15 +09:00
|
|
|
else
|
2024-11-18 02:49:48 +09:00
|
|
|
(extendDBusDefault app.id default);
|
|
|
|
system_bus = app.dbus.system;
|
2024-11-06 04:25:15 +09:00
|
|
|
};
|
2024-11-18 02:49:48 +09:00
|
|
|
command = if app.command == null then app.name else app.command;
|
|
|
|
script = if app.script == null then ("exec " + command + " $@") else app.script;
|
2024-11-06 04:25:15 +09:00
|
|
|
enablements =
|
2024-11-18 02:49:48 +09:00
|
|
|
with app.capability;
|
2024-11-06 04:25:15 +09:00
|
|
|
(if wayland then 1 else 0)
|
|
|
|
+ (if x11 then 2 else 0)
|
|
|
|
+ (if dbus then 4 else 0)
|
|
|
|
+ (if pulse then 8 else 0);
|
|
|
|
conf = {
|
2024-11-18 02:49:48 +09:00
|
|
|
inherit (app) id;
|
2024-11-06 04:25:15 +09:00
|
|
|
command = [
|
2024-11-18 02:49:48 +09:00
|
|
|
(pkgs.writeScript "${app.name}-start" ''
|
2024-11-06 14:09:41 +09:00
|
|
|
#!${pkgs.zsh}${pkgs.zsh.shellPath}
|
|
|
|
${script}
|
|
|
|
'')
|
2024-11-06 04:25:15 +09:00
|
|
|
];
|
|
|
|
confinement = {
|
2024-11-18 02:49:48 +09:00
|
|
|
app_id = aid;
|
|
|
|
inherit (app) groups;
|
|
|
|
username = "u${toString fid}_a${toString aid}";
|
|
|
|
home = "${cfg.stateDir}/${toString fid}/${toString aid}";
|
2024-11-06 04:25:15 +09:00
|
|
|
sandbox = {
|
2024-11-18 02:49:48 +09:00
|
|
|
inherit (app)
|
2024-11-06 04:25:15 +09:00
|
|
|
userns
|
|
|
|
net
|
|
|
|
dev
|
|
|
|
env
|
|
|
|
;
|
2024-11-18 02:49:48 +09:00
|
|
|
map_real_uid = app.mapRealUid;
|
2024-11-06 04:25:15 +09:00
|
|
|
filesystem =
|
|
|
|
[
|
|
|
|
{ src = "/bin"; }
|
|
|
|
{ src = "/usr/bin"; }
|
|
|
|
{ src = "/nix/store"; }
|
|
|
|
{ src = "/run/current-system"; }
|
|
|
|
{
|
|
|
|
src = "/sys/block";
|
|
|
|
require = false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
src = "/sys/bus";
|
|
|
|
require = false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
src = "/sys/class";
|
|
|
|
require = false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
src = "/sys/dev";
|
|
|
|
require = false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
src = "/sys/devices";
|
|
|
|
require = false;
|
|
|
|
}
|
|
|
|
]
|
2024-11-18 02:49:48 +09:00
|
|
|
++ optionals app.nix [
|
2024-11-06 04:25:15 +09:00
|
|
|
{ src = "/nix/var"; }
|
|
|
|
{ src = "/var/db/nix-channels"; }
|
|
|
|
]
|
2024-11-18 02:49:48 +09:00
|
|
|
++ optionals (if app.gpu != null then app.gpu else app.capability.wayland || app.capability.x11) [
|
2024-11-06 04:25:15 +09:00
|
|
|
{ src = "/run/opengl-driver"; }
|
|
|
|
{
|
|
|
|
src = "/dev/dri";
|
|
|
|
dev = true;
|
|
|
|
}
|
|
|
|
]
|
2024-11-18 02:49:48 +09:00
|
|
|
++ app.extraPaths;
|
2024-11-06 04:25:15 +09:00
|
|
|
auto_etc = true;
|
|
|
|
override = [ "/var/run/nscd" ];
|
|
|
|
};
|
|
|
|
inherit enablements;
|
|
|
|
inherit (dbusConfig) session_bus system_bus;
|
|
|
|
};
|
|
|
|
};
|
2024-09-04 17:03:21 +09:00
|
|
|
in
|
2024-11-18 02:49:48 +09:00
|
|
|
pkgs.writeShellScriptBin app.name ''
|
|
|
|
exec fortify app ${pkgs.writeText "fortify-${app.name}.json" (builtins.toJSON conf)} $@
|
|
|
|
''
|
|
|
|
) cfg.apps;
|
|
|
|
in
|
|
|
|
foldr (
|
|
|
|
app: acc:
|
2024-09-04 17:03:21 +09:00
|
|
|
let
|
2024-11-18 02:49:48 +09:00
|
|
|
pkg = if app.share != null then app.share else pkgs.${app.name};
|
2024-11-08 02:05:47 +09:00
|
|
|
copy = source: "[ -d '${source}' ] && cp -Lrv '${source}' $out/share || true";
|
2024-09-04 17:03:21 +09:00
|
|
|
in
|
2024-11-18 02:49:48 +09:00
|
|
|
optional (app.capability.wayland || app.capability.x11) (
|
|
|
|
pkgs.runCommand "${app.name}-share" { } ''
|
|
|
|
mkdir -p $out/share
|
|
|
|
${copy "${pkg}/share/applications"}
|
|
|
|
${copy "${pkg}/share/icons"}
|
|
|
|
${copy "${pkg}/share/man"}
|
|
|
|
|
|
|
|
substituteInPlace $out/share/applications/* \
|
|
|
|
--replace-warn '${pkg}/bin/' "" \
|
|
|
|
--replace-warn '${pkg}/libexec/' ""
|
|
|
|
''
|
|
|
|
)
|
|
|
|
++ acc
|
|
|
|
) (wrappers ++ [ cfg.package ]) cfg.apps;
|
|
|
|
}) cfg.users;
|
2024-09-04 17:03:21 +09:00
|
|
|
in
|
2024-11-18 02:49:48 +09:00
|
|
|
{
|
|
|
|
useUserPackages = false; # prevent users.users entries from being added
|
|
|
|
|
|
|
|
users = foldlAttrs (
|
|
|
|
acc: _: fid:
|
|
|
|
mergeAttrsList (
|
|
|
|
# aid 0 is reserved
|
|
|
|
imap1 (aid: app: {
|
|
|
|
"u${toString fid}_a${toString aid}" = app.extraConfig // {
|
|
|
|
home.packages = app.packages;
|
|
|
|
};
|
|
|
|
}) cfg.apps
|
|
|
|
)
|
|
|
|
// acc
|
|
|
|
) privPackages cfg.users;
|
|
|
|
};
|
2024-09-04 17:03:21 +09:00
|
|
|
};
|
|
|
|
}
|