hst/enablement: editor friendly enablement adaptor
Some checks failed
Test / Sandbox (push) Successful in 2m10s
Test / Hakurei (push) Failing after 2m32s
Test / Sandbox (race detector) (push) Successful in 4m15s
Test / Hakurei (race detector) (push) Failing after 4m19s
Test / Hpkg (push) Failing after 17m28s
Test / Flake checks (push) Has been skipped
Test / Create distribution (push) Successful in 35s

Having the bit field value here (in decimal, no less) is unfriendly to text editors. Use a bunch of booleans here to improve ease of use.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-08-15 04:57:37 +09:00
parent 9ed3ba85ea
commit 4c7c54d701
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
17 changed files with 231 additions and 39 deletions

View File

@ -124,18 +124,20 @@ func buildCommand(out io.Writer) command.Command {
config.Data = a config.Data = a
} }
var e system.Enablement
if wayland { if wayland {
config.Enablements |= system.EWayland e |= system.EWayland
} }
if x11 { if x11 {
config.Enablements |= system.EX11 e |= system.EX11
} }
if dBus { if dBus {
config.Enablements |= system.EDBus e |= system.EDBus
} }
if pulse { if pulse {
config.Enablements |= system.EPulse e |= system.EPulse
} }
config.Enablements = hst.NewEnablements(e)
// parse D-Bus config file from flags if applicable // parse D-Bus config file from flags if applicable
if dBus { if dBus {

View File

@ -78,7 +78,7 @@ func printShowInstance(
} else { } else {
t.Printf(" Identity:\t%d\n", config.Identity) t.Printf(" Identity:\t%d\n", config.Identity)
} }
t.Printf(" Enablements:\t%s\n", config.Enablements.String()) t.Printf(" Enablements:\t%s\n", config.Enablements.Unwrap().String())
if len(config.Groups) > 0 { if len(config.Groups) > 0 {
t.Printf(" Groups:\t%s\n", strings.Join(config.Groups, ", ")) t.Printf(" Groups:\t%s\n", strings.Join(config.Groups, ", "))
} }

View File

@ -195,7 +195,11 @@ App
"--enable-features=UseOzonePlatform", "--enable-features=UseOzonePlatform",
"--ozone-platform=wayland" "--ozone-platform=wayland"
], ],
"enablements": 13, "enablements": {
"wayland": true,
"dbus": true,
"pulse": true
},
"session_bus": { "session_bus": {
"see": null, "see": null,
"talk": [ "talk": [
@ -339,7 +343,11 @@ App
"--enable-features=UseOzonePlatform", "--enable-features=UseOzonePlatform",
"--ozone-platform=wayland" "--ozone-platform=wayland"
], ],
"enablements": 13, "enablements": {
"wayland": true,
"dbus": true,
"pulse": true
},
"session_bus": { "session_bus": {
"see": null, "see": null,
"talk": [ "talk": [
@ -537,7 +545,11 @@ func Test_printPs(t *testing.T) {
"--enable-features=UseOzonePlatform", "--enable-features=UseOzonePlatform",
"--ozone-platform=wayland" "--ozone-platform=wayland"
], ],
"enablements": 13, "enablements": {
"wayland": true,
"dbus": true,
"pulse": true
},
"session_bus": { "session_bus": {
"see": null, "see": null,
"talk": [ "talk": [

View File

@ -8,7 +8,6 @@ import (
"hakurei.app/container" "hakurei.app/container"
"hakurei.app/container/seccomp" "hakurei.app/container/seccomp"
"hakurei.app/hst" "hakurei.app/hst"
"hakurei.app/system"
"hakurei.app/system/dbus" "hakurei.app/system/dbus"
) )
@ -41,7 +40,7 @@ type appInfo struct {
// passed through to [hst.Config] // passed through to [hst.Config]
SessionBus *dbus.Config `json:"session_bus,omitempty"` SessionBus *dbus.Config `json:"session_bus,omitempty"`
// passed through to [hst.Config] // passed through to [hst.Config]
Enablements system.Enablement `json:"enablements"` Enablements *hst.Enablements `json:"enablements,omitempty"`
// passed through to [hst.Config] // passed through to [hst.Config]
Multiarch bool `json:"multiarch,omitempty"` Multiarch bool `json:"multiarch,omitempty"`

View File

@ -3,7 +3,6 @@ package hst
import ( import (
"hakurei.app/container" "hakurei.app/container"
"hakurei.app/system"
"hakurei.app/system/dbus" "hakurei.app/system/dbus"
) )
@ -24,7 +23,7 @@ type Config struct {
Args []string `json:"args"` Args []string `json:"args"`
// system services to make available in the container // system services to make available in the container
Enablements system.Enablement `json:"enablements"` Enablements *Enablements `json:"enablements,omitempty"`
// session D-Bus proxy configuration; // session D-Bus proxy configuration;
// nil makes session bus proxy assume built-in defaults // nil makes session bus proxy assume built-in defaults

69
hst/enablement.go Normal file
View File

@ -0,0 +1,69 @@
package hst
import (
"encoding/json"
"syscall"
"hakurei.app/system"
)
// NewEnablements returns the address of [system.Enablement] as [Enablements].
func NewEnablements(e system.Enablement) *Enablements { return (*Enablements)(&e) }
// enablementsJSON is the [json] representation of the [system.Enablement] bit field.
type enablementsJSON struct {
Wayland bool `json:"wayland,omitempty"`
X11 bool `json:"x11,omitempty"`
DBus bool `json:"dbus,omitempty"`
Pulse bool `json:"pulse,omitempty"`
}
// Enablements is the [json] adapter for [system.Enablement].
type Enablements system.Enablement
// Unwrap returns the underlying [system.Enablement].
func (e *Enablements) Unwrap() system.Enablement {
if e == nil {
return 0
}
return system.Enablement(*e)
}
func (e *Enablements) MarshalJSON() ([]byte, error) {
if e == nil {
return nil, syscall.EINVAL
}
return json.Marshal(&enablementsJSON{
Wayland: system.Enablement(*e)&system.EWayland != 0,
X11: system.Enablement(*e)&system.EX11 != 0,
DBus: system.Enablement(*e)&system.EDBus != 0,
Pulse: system.Enablement(*e)&system.EPulse != 0,
})
}
func (e *Enablements) UnmarshalJSON(data []byte) error {
if e == nil {
return syscall.EINVAL
}
v := new(enablementsJSON)
if err := json.Unmarshal(data, &v); err != nil {
return err
}
var ve system.Enablement
if v.Wayland {
ve |= system.EWayland
}
if v.X11 {
ve |= system.EX11
}
if v.DBus {
ve |= system.EDBus
}
if v.Pulse {
ve |= system.EPulse
}
*e = Enablements(ve)
return nil
}

108
hst/enablement_test.go Normal file
View File

@ -0,0 +1,108 @@
package hst_test
import (
"encoding/json"
"errors"
"syscall"
"testing"
"hakurei.app/hst"
"hakurei.app/system"
)
func TestEnablements(t *testing.T) {
testCases := []struct {
name string
e *hst.Enablements
data string
sData string
}{
{"nil", nil, "null", `{"value":null,"magic":3236757504}`},
{"zero", hst.NewEnablements(0), `{}`, `{"value":{},"magic":3236757504}`},
{"wayland", hst.NewEnablements(system.EWayland), `{"wayland":true}`, `{"value":{"wayland":true},"magic":3236757504}`},
{"x11", hst.NewEnablements(system.EX11), `{"x11":true}`, `{"value":{"x11":true},"magic":3236757504}`},
{"dbus", hst.NewEnablements(system.EDBus), `{"dbus":true}`, `{"value":{"dbus":true},"magic":3236757504}`},
{"pulse", hst.NewEnablements(system.EPulse), `{"pulse":true}`, `{"value":{"pulse":true},"magic":3236757504}`},
{"all", hst.NewEnablements(system.EWayland | system.EX11 | system.EDBus | system.EPulse), `{"wayland":true,"x11":true,"dbus":true,"pulse":true}`, `{"value":{"wayland":true,"x11":true,"dbus":true,"pulse":true},"magic":3236757504}`},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
if got, err := json.Marshal(tc.e); err != nil {
t.Fatalf("Marshal: error = %v", err)
} else if string(got) != tc.data {
t.Errorf("Marshal:\n%s, want\n%s", string(got), tc.data)
}
if got, err := json.Marshal(struct {
Value *hst.Enablements `json:"value"`
Magic int `json:"magic"`
}{tc.e, syscall.MS_MGC_VAL}); err != nil {
t.Fatalf("Marshal: error = %v", err)
} else if string(got) != tc.sData {
t.Errorf("Marshal:\n%s, want\n%s", string(got), tc.sData)
}
})
t.Run("unmarshal", func(t *testing.T) {
{
got := new(hst.Enablements)
if err := json.Unmarshal([]byte(tc.data), &got); err != nil {
t.Fatalf("Unmarshal: error = %v", err)
}
if tc.e == nil {
if got != nil {
t.Errorf("Unmarshal: %v", got)
}
} else if *got != *tc.e {
t.Errorf("Unmarshal: %v, want %v", got, tc.e)
}
}
{
got := *(new(struct {
Value *hst.Enablements `json:"value"`
Magic int `json:"magic"`
}))
if err := json.Unmarshal([]byte(tc.sData), &got); err != nil {
t.Fatalf("Unmarshal: error = %v", err)
}
if tc.e == nil {
if got.Value != nil {
t.Errorf("Unmarshal: %v", got)
}
} else if *got.Value != *tc.e {
t.Errorf("Unmarshal: %v, want %v", got.Value, tc.e)
}
}
})
})
}
t.Run("unwrap", func(t *testing.T) {
t.Run("nil", func(t *testing.T) {
if got := (*hst.Enablements)(nil).Unwrap(); got != 0 {
t.Errorf("Unwrap: %v", got)
}
})
t.Run("val", func(t *testing.T) {
if got := hst.NewEnablements(system.EWayland | system.EPulse).Unwrap(); got != system.EWayland|system.EPulse {
t.Errorf("Unwrap: %v", got)
}
})
})
t.Run("passthrough", func(t *testing.T) {
if _, err := (*hst.Enablements)(nil).MarshalJSON(); !errors.Is(err, syscall.EINVAL) {
t.Errorf("MarshalJSON: error = %v", err)
}
if err := (*hst.Enablements)(nil).UnmarshalJSON(nil); !errors.Is(err, syscall.EINVAL) {
t.Errorf("UnmarshalJSON: error = %v", err)
}
if err := new(hst.Enablements).UnmarshalJSON([]byte{}); err == nil {
t.Errorf("UnmarshalJSON: error = %v", err)
}
})
}

View File

@ -21,7 +21,7 @@ func Template() *Config {
"--ozone-platform=wayland", "--ozone-platform=wayland",
}, },
Enablements: system.EWayland | system.EDBus | system.EPulse, Enablements: NewEnablements(system.EWayland | system.EDBus | system.EPulse),
SessionBus: &dbus.Config{ SessionBus: &dbus.Config{
See: nil, See: nil,

View File

@ -18,7 +18,11 @@ func TestTemplate(t *testing.T) {
"--enable-features=UseOzonePlatform", "--enable-features=UseOzonePlatform",
"--ozone-platform=wayland" "--ozone-platform=wayland"
], ],
"enablements": 13, "enablements": {
"wayland": true,
"dbus": true,
"pulse": true
},
"session_bus": { "session_bus": {
"see": null, "see": null,
"talk": [ "talk": [

View File

@ -23,7 +23,7 @@ var testCasesNixos = []sealTestCase{
&hst.Config{ &hst.Config{
ID: "org.chromium.Chromium", ID: "org.chromium.Chromium",
Path: m("/nix/store/yqivzpzzn7z5x0lq9hmbzygh45d8rhqd-chromium-start"), Path: m("/nix/store/yqivzpzzn7z5x0lq9hmbzygh45d8rhqd-chromium-start"),
Enablements: system.EWayland | system.EDBus | system.EPulse, Enablements: hst.NewEnablements(system.EWayland | system.EDBus | system.EPulse),
Shell: m("/run/current-system/sw/bin/zsh"), Shell: m("/run/current-system/sw/bin/zsh"),
Container: &hst.ContainerConfig{ Container: &hst.ContainerConfig{

View File

@ -106,7 +106,7 @@ var testCasesPd = []sealTestCase{
}, },
Filter: true, Filter: true,
}, },
Enablements: system.EWayland | system.EDBus | system.EPulse, Enablements: hst.NewEnablements(system.EWayland | system.EDBus | system.EPulse),
}, },
state.ID{ state.ID{
0xeb, 0xf0, 0x83, 0xd1, 0xeb, 0xf0, 0x83, 0xd1,

View File

@ -64,7 +64,7 @@ func (seal *outcome) Run(rs *RunState) error {
// accumulate enablements of remaining launchers // accumulate enablements of remaining launchers
for i, s := range states { for i, s := range states {
if s.Config != nil { if s.Config != nil {
rt |= s.Config.Enablements rt |= s.Config.Enablements.Unwrap()
} else { } else {
log.Printf("state entry %d does not contain config", i) log.Printf("state entry %d does not contain config", i)
} }

View File

@ -247,7 +247,7 @@ func (seal *outcome) finalise(ctx context.Context, sys sys.State, config *hst.Co
} }
// bind GPU stuff // bind GPU stuff
if config.Enablements&(system.EX11|system.EWayland) != 0 { if config.Enablements.Unwrap()&(system.EX11|system.EWayland) != 0 {
conf.Filesystem = append(conf.Filesystem, hst.FilesystemConfigJSON{FilesystemConfig: &hst.FSBind{Src: container.AbsFHSDev.Append("dri"), Device: true, Optional: true}}) conf.Filesystem = append(conf.Filesystem, hst.FilesystemConfigJSON{FilesystemConfig: &hst.FSBind{Src: container.AbsFHSDev.Append("dri"), Device: true, Optional: true}})
} }
// opportunistically bind kvm // opportunistically bind kvm
@ -348,7 +348,7 @@ func (seal *outcome) finalise(ctx context.Context, sys sys.State, config *hst.Co
seal.env[term] = t seal.env[term] = t
} }
if config.Enablements&system.EWayland != 0 { if config.Enablements.Unwrap()&system.EWayland != 0 {
// outer wayland socket (usually `/run/user/%d/wayland-%d`) // outer wayland socket (usually `/run/user/%d/wayland-%d`)
var socketPath *container.Absolute var socketPath *container.Absolute
if name, ok := sys.LookupEnv(wayland.WaylandDisplay); !ok { if name, ok := sys.LookupEnv(wayland.WaylandDisplay); !ok {
@ -381,7 +381,7 @@ func (seal *outcome) finalise(ctx context.Context, sys sys.State, config *hst.Co
} }
} }
if config.Enablements&system.EX11 != 0 { if config.Enablements.Unwrap()&system.EX11 != 0 {
if d, ok := sys.LookupEnv(display); !ok { if d, ok := sys.LookupEnv(display); !ok {
return hlog.WrapErr(ErrXDisplay, return hlog.WrapErr(ErrXDisplay,
"DISPLAY is not set") "DISPLAY is not set")
@ -393,7 +393,7 @@ func (seal *outcome) finalise(ctx context.Context, sys sys.State, config *hst.Co
} }
} }
if config.Enablements&system.EPulse != 0 { if config.Enablements.Unwrap()&system.EPulse != 0 {
// PulseAudio runtime directory (usually `/run/user/%d/pulse`) // PulseAudio runtime directory (usually `/run/user/%d/pulse`)
pulseRuntimeDir := share.sc.RuntimePath.Append("pulse") pulseRuntimeDir := share.sc.RuntimePath.Append("pulse")
// PulseAudio socket (usually `/run/user/%d/pulse/native`) // PulseAudio socket (usually `/run/user/%d/pulse/native`)
@ -442,7 +442,7 @@ func (seal *outcome) finalise(ctx context.Context, sys sys.State, config *hst.Co
} }
} }
if config.Enablements&system.EDBus != 0 { if config.Enablements.Unwrap()&system.EDBus != 0 {
// ensure dbus session bus defaults // ensure dbus session bus defaults
if config.SessionBus == nil { if config.SessionBus == nil {
config.SessionBus = dbus.NewConfig(config.ID, true, true) config.SessionBus = dbus.NewConfig(config.ID, true, true)

View File

@ -102,8 +102,7 @@ in
}; };
command = if app.command == null then app.name else app.command; command = if app.command == null then app.name else app.command;
script = if app.script == null then ("exec " + command + " $@") else app.script; script = if app.script == null then ("exec " + command + " $@") else app.script;
enablements = with app.capability; (if wayland then 1 else 0) + (if x11 then 2 else 0) + (if dbus then 4 else 0) + (if pulse then 8 else 0); isGraphical = if app.gpu != null then app.gpu else app.enablements.wayland || app.enablements.x11;
isGraphical = if app.gpu != null then app.gpu else app.capability.wayland || app.capability.x11;
conf = { conf = {
path = path =
@ -116,7 +115,7 @@ in
app.path; app.path;
args = if app.args == null then [ "${app.name}-start" ] else app.args; args = if app.args == null then [ "${app.name}-start" ] else app.args;
inherit id enablements; inherit id;
inherit (dbusConfig) session_bus system_bus; inherit (dbusConfig) session_bus system_bus;
direct_wayland = app.insecureWayland; direct_wayland = app.insecureWayland;
@ -125,7 +124,7 @@ in
data = getsubhome fid app.identity; data = getsubhome fid app.identity;
inherit (cfg) shell; inherit (cfg) shell;
inherit (app) identity groups; inherit (app) identity groups enablements;
container = { container = {
inherit (app) inherit (app)
@ -226,7 +225,7 @@ in
pkg = if app.share != null then app.share else pkgs.${app.name}; pkg = if app.share != null then app.share else pkgs.${app.name};
copy = source: "[ -d '${source}' ] && cp -Lrv '${source}' $out/share || true"; copy = source: "[ -d '${source}' ] && cp -Lrv '${source}' $out/share || true";
in in
optional (app.capability.wayland || app.capability.x11) ( optional (app.enablements.wayland || app.enablements.x11) (
pkgs.runCommand "${app.name}-share" { } '' pkgs.runCommand "${app.name}-share" { } ''
mkdir -p $out/share mkdir -p $out/share
${copy "${pkg}/share/applications"} ${copy "${pkg}/share/applications"}

View File

@ -209,9 +209,9 @@ in
''; '';
}; };
capability = { enablements = {
wayland = mkOption { wayland = mkOption {
type = bool; type = nullOr bool;
default = true; default = true;
description = '' description = ''
Whether to share the Wayland socket. Whether to share the Wayland socket.
@ -219,7 +219,7 @@ in
}; };
x11 = mkOption { x11 = mkOption {
type = bool; type = nullOr bool;
default = false; default = false;
description = '' description = ''
Whether to share the X11 socket and allow connection. Whether to share the X11 socket and allow connection.
@ -227,7 +227,7 @@ in
}; };
dbus = mkOption { dbus = mkOption {
type = bool; type = nullOr bool;
default = true; default = true;
description = '' description = ''
Whether to proxy D-Bus. Whether to proxy D-Bus.
@ -235,7 +235,7 @@ in
}; };
pulse = mkOption { pulse = mkOption {
type = bool; type = nullOr bool;
default = true; default = true;
description = '' description = ''
Whether to share the PulseAudio socket and cookie. Whether to share the PulseAudio socket and cookie.

View File

@ -126,7 +126,7 @@
wayland-utils wayland-utils
]; ];
command = "foot"; command = "foot";
capability = { enablements = {
dbus = false; dbus = false;
pulse = false; pulse = false;
}; };
@ -141,7 +141,7 @@
share = pkgs.foot; share = pkgs.foot;
packages = [ ]; packages = [ ];
command = "foot"; command = "foot";
capability = { enablements = {
dbus = false; dbus = false;
pulse = false; pulse = false;
}; };
@ -154,7 +154,7 @@
share = pkgs.foot; share = pkgs.foot;
packages = [ pkgs.foot ]; packages = [ pkgs.foot ];
command = "foot"; command = "foot";
capability.dbus = false; enablements.dbus = false;
}; };
"cat.gensokyo.extern.Alacritty.x11" = { "cat.gensokyo.extern.Alacritty.x11" = {
@ -171,7 +171,7 @@
mesa-demos mesa-demos
]; ];
command = "alacritty"; command = "alacritty";
capability = { enablements = {
wayland = false; wayland = false;
x11 = true; x11 = true;
dbus = false; dbus = false;
@ -192,7 +192,7 @@
wayland-utils wayland-utils
]; ];
command = "foot"; command = "foot";
capability = { enablements = {
dbus = false; dbus = false;
pulse = false; pulse = false;
}; };
@ -204,7 +204,7 @@
verbose = true; verbose = true;
share = pkgs.strace; share = pkgs.strace;
command = "strace true"; command = "strace true";
capability = { enablements = {
wayland = false; wayland = false;
x11 = false; x11 = false;
dbus = false; dbus = false;

View File

@ -13,7 +13,7 @@
share = pkgs.foot; share = pkgs.foot;
packages = [ pkgs.foot ]; packages = [ pkgs.foot ];
command = "foot"; command = "foot";
capability = { enablements = {
dbus = false; dbus = false;
pulse = false; pulse = false;
}; };