stockholm/lass/3modules/xjail.nix

174 lines
5.1 KiB
Nix
Raw Normal View History

2018-04-01 14:17:45 +00:00
{ config, pkgs, lib, ... }:
2018-03-28 19:12:53 +00:00
with import <stockholm/lib>;
{
options.lass.xjail = mkOption {
type = types.attrsOf (types.submodule ({ config, ...}: {
options = {
2018-04-01 14:17:45 +00:00
name = mkOption {
type = types.str;
2018-04-01 14:17:45 +00:00
default = config._module.args.name;
};
2018-03-28 19:12:53 +00:00
user = mkOption {
type = types.str;
2018-04-01 14:17:45 +00:00
default = config.name;
2018-03-28 19:12:53 +00:00
};
groups = mkOption {
type = types.listOf types.str;
default = [];
};
2018-04-01 14:17:45 +00:00
from = mkOption {
type = types.str;
2018-04-01 14:17:45 +00:00
default = "lass";
2018-03-28 19:12:53 +00:00
};
display = mkOption {
type = types.str;
2018-08-09 12:38:06 +00:00
default = toString (genid_uint31 config._module.args.name);
2018-03-28 19:12:53 +00:00
};
2018-04-01 14:17:45 +00:00
dpi = mkOption {
type = types.int;
default = 90;
};
extraXephyrArgs = mkOption {
type = types.str;
default = "";
};
extraVglrunArgs = mkOption {
type = types.str;
default = "";
};
2018-03-28 19:12:53 +00:00
script = mkOption {
type = types.path;
default = pkgs.writeScript "echo_lol" "echo lol";
};
2018-06-27 21:59:14 +00:00
vglrun = mkOption {
type = types.bool;
default = false;
};
2018-04-01 14:17:45 +00:00
wm = mkOption {
#TODO find type
type = types.str;
2018-06-19 19:23:35 +00:00
default = "${pkgs.writeHaskellPackage "xephyrify-xmonad" {
2018-04-01 14:17:45 +00:00
executables.xmonad = {
extra-depends = [
"containers"
"unix"
"xmonad"
];
text = /* haskell */ ''
module Main where
import XMonad
import Data.Monoid
import System.Posix.Process (executeFile)
import qualified Data.Map as Map
main :: IO ()
main = do
xmonad def
{ workspaces = [ "1" ]
, layoutHook = myLayoutHook
, keys = myKeys
, normalBorderColor = "#000000"
, focusedBorderColor = "#000000"
, handleEventHook = myEventHook
}
myEventHook :: Event -> X All
myEventHook (ConfigureEvent { ev_event_type = 22 }) = do
spawn "${pkgs.xorg.xrandr}/bin/xrandr >/dev/null 2>&1"
return (All True)
myEventHook _ = do
return (All True)
myLayoutHook = Full
myKeys _ = Map.fromList []
'';
};
}}/bin/xmonad";
2018-03-28 19:12:53 +00:00
};
};
}));
default = {};
};
options.lass.xjail-bins = mkOption {
type = types.attrsOf types.path;
};
# implementation
2018-04-01 14:17:45 +00:00
config = let
scripts = mapAttrs' (name: cfg:
let
newOrExisting = pkgs.writeDash "${cfg.name}-existing" ''
DISPLAY=:${cfg.display} ${pkgs.xorg.xrandr}/bin/xrandr
if test $? -eq 0; then
echo using existing xephyr
${sudo_} "$@"
else
echo starting new xephyr
${xephyr_} "$@"
fi
'';
xephyr_ = pkgs.writeDash "${cfg.name}-xephyr" ''
${pkgs.xorg.xorgserver}/bin/Xephyr -br -ac -reset -terminate -resizeable -nolisten local -dpi ${toString cfg.dpi} ${cfg.extraXephyrArgs} :${cfg.display} &
XEPHYR_PID=$!
DISPLAY=:${cfg.display} ${cfg.wm} &
WM_PID=$!
${sudo_} "$@"
${pkgs.coreutils}/bin/kill $WM_PID
${pkgs.coreutils}/bin/kill $XEPHYR_PID
'';
2018-10-23 13:47:06 +00:00
# TODO fix xephyr which doesn't honor resizes anymore
2018-06-27 21:59:14 +00:00
sudo_ = pkgs.writeDash "${cfg.name}-sudo" (if cfg.vglrun then ''
2018-04-01 14:17:45 +00:00
/var/run/wrappers/bin/sudo -u ${cfg.name} -i ${vglrun_} "$@"
2018-06-27 21:59:14 +00:00
'' else ''
2018-10-23 13:47:06 +00:00
#/var/run/wrappers/bin/sudo -u ${cfg.name} -i env DISPLAY=:${cfg.display} ${cfg.script} "$@"
/var/run/wrappers/bin/sudo -u ${cfg.name} -i ${cfg.script} "$@"
2018-06-27 21:59:14 +00:00
'');
2018-04-01 14:17:45 +00:00
vglrun_ = pkgs.writeDash "${cfg.name}-vglrun" ''
DISPLAY=:${cfg.display} ${pkgs.virtualgl}/bin/vglrun ${cfg.extraVglrunArgs} ${cfg.script} "$@"
'';
in nameValuePair name {
existing = newOrExisting;
xephyr = xephyr_;
sudo = sudo_;
vglrun = vglrun_;
}
) config.lass.xjail;
in {
2018-03-28 19:12:53 +00:00
users.users = mapAttrs' (_: cfg:
nameValuePair cfg.name {
2018-12-03 08:47:35 +00:00
uid = genid_uint31 cfg.name;
2018-03-28 19:12:53 +00:00
home = "/home/${cfg.name}";
useDefaultShell = true;
createHome = true;
extraGroups = cfg.groups;
}
) config.lass.xjail;
users.groups = mapAttrs' (_: cfg:
nameValuePair cfg.name {
members = [
cfg.name
cfg.from
];
}
) config.lass.xjail;
security.sudo.extraConfig = (concatStringsSep "\n" (mapAttrsToList (_: cfg:
# TODO allow just the right script with sudo
"${cfg.from} ALL=(${cfg.name}) NOPASSWD: ALL"
) config.lass.xjail));
lass.xjail-bins = mapAttrs' (name: cfg:
2018-04-01 14:17:45 +00:00
nameValuePair name (pkgs.writeScriptBin cfg.name ''
2018-10-23 13:47:06 +00:00
${scripts.${name}.sudo} "$@"
2018-03-28 19:12:53 +00:00
'')
) config.lass.xjail;
};
}