bwbox/lib/config.nim

76 lines
2.5 KiB
Nim
Raw Permalink Normal View History

2021-06-19 16:33:47 +02:00
import sequtils
2021-05-18 22:10:35 +02:00
import options
2021-06-19 16:33:47 +02:00
import bwrap
import utils
import json
2021-12-27 16:39:18 +01:00
import os
2021-05-18 22:10:35 +02:00
type Link* = object
src*: string
dst*: string
type Config* = object
2021-06-19 16:33:47 +02:00
extends*: Option[string]
mount*: Option[seq[string]]
romount*: Option[seq[string]]
symlinks*: Option[seq[Link]]
2021-06-20 14:09:30 +02:00
mountcwd*: Option[bool]
privileged*: Option[bool]
sethostname*: Option[bool]
2021-06-27 16:46:29 +02:00
allowdri*: Option[bool]
dbus*: Option[bool]
dbussee*: Option[seq[string]]
dbustalk*: Option[seq[string]]
dbusown*: Option[seq[string]]
dbuscall*: Option[seq[string]]
dbusbroadcast*: Option[seq[string]]
2021-12-27 16:39:18 +01:00
devmount*: Option[seq[string]]
2021-06-19 16:33:47 +02:00
proc applyConfig*(call: var BwrapCall, config: Config) =
for mount in config.mount.get(@[]):
call.addMount("--bind", checkRelativePath(mount))
for mount in config.romount.get(@[]):
call.addMount("--ro-bind", checkRelativePath(mount))
for symlink in config.symlinks.get(@[]):
call.addArg("--symlink", symlink.src, symlink.dst)
2021-12-27 16:39:18 +01:00
for device in config.devmount.get(@[]):
call.addArg("--dev-bind", device, device)
if config.mountcwd.get(false):
call
.addMount("--bind", getCurrentDir())
.addArg("--chdir", getCurrentDir())
2021-06-19 16:33:47 +02:00
proc loadConfig*(path: string): Config =
return readFile(path)
.parseJson()
.to(Config)
proc extendConfig*(config: var Config): Config {.discardable.} =
if config.extends.isNone:
return
var eConf = loadConfig(getProfilePath(config.extends.unsafeGet))
eConf.extendConfig()
2021-06-27 16:46:29 +02:00
# todo: replace using macro / templates
2021-06-19 16:33:47 +02:00
config.mount = some(config.mount.get(@[]).concat(eConf.mount.get(@[])))
config.romount = some(config.romount.get(@[]).concat(eConf.romount.get(@[])))
config.symlinks = some(config.symlinks.get(@[]).concat(eConf.symlinks.get(@[])))
2021-06-20 14:09:30 +02:00
config.mountcwd = some(config.mountcwd.get(eConf.mountcwd.get(false)))
config.sethostname = some(config.sethostname.get(eConf.sethostname.get(false)))
2021-06-27 16:46:29 +02:00
config.allowdri = some(config.allowdri.get(eConf.allowdri.get(false)))
2021-12-27 16:39:18 +01:00
config.devmount = some(config.devmount.get(eConf.devmount.get(@[])))
2021-06-27 16:46:29 +02:00
config.dbus = some(config.dbus.get(eConf.dbus.get(false)))
config.dbussee = some(config.dbussee.get(@[]).concat(eConf.dbussee.get(@[])))
config.dbustalk = some(config.dbustalk.get(@[]).concat(eConf.dbustalk.get(@[])))
config.dbusown = some(config.dbusown.get(@[]).concat(eConf.dbusown.get(@[])))
config.dbuscall = some(config.dbuscall.get(@[]).concat(eConf.dbuscall.get(@[])))
config.dbusbroadcast = some(config.dbusbroadcast.get(@[]).concat(eConf.dbusbroadcast.get(@[])))
2021-06-19 16:33:47 +02:00
return config