Fix time-zone combinator on missing /etc/localtime

Thanks ~charlie-collard for reporting this

Fixes https://todo.sr.ht/~alexdavid/jail.nix/2
This commit is contained in:
Alex David 2025-10-14 19:35:34 -07:00
commit d596e0bc81
2 changed files with 31 additions and 6 deletions

View file

@ -1,9 +1,8 @@
{ combinators, ... }:
let
inherit (combinators)
compose
add-runtime
include-once
unsafe-add-raw-args
;
in
{
@ -11,8 +10,12 @@ in
doc = ''
Exposes your timezone.
'';
impl = include-once "time-zone" (compose [
(unsafe-add-raw-args "--ro-bind \"$(realpath /etc/localtime)\" \"$(readlink /etc/localtime)\"")
(unsafe-add-raw-args "--symlink \"$(readlink /etc/localtime)\" /etc/localtime")
]);
impl = include-once "time-zone" (add-runtime ''
if [ -L /etc/localtime ]; then
RUNTIME_ARGS+=(
--ro-bind "$(realpath /etc/localtime)" "$(readlink /etc/localtime)"
--symlink "$(readlink /etc/localtime)" /etc/localtime
)
fi
'');
}

View file

@ -275,6 +275,28 @@ spec = parallel $ inTestM $ do
|]
`shouldOutput` "EST\n"
it "does not fail if /etc/localtime does not exist" $ do
[i|
let
fakeEtc = pkgs.runCommand "fake-etc" {} ''
mkdir -p $out/etc
ln -s ${pkgs.tzdata}/share/zoneinfo $out/etc/zoneinfo
'';
withFakeTimeZoneOnNixos = program:
jail program.name program (c: [
(c.add-pkg-deps [ fakeEtc ])
(c.unsafe-add-raw-args "--symlink ${fakeEtc}/etc /etc/static")
(c.unsafe-add-raw-args "--symlink /etc/static/zoneinfo /etc/zoneinfo")
]);
in
withFakeTimeZoneOnNixos (
jail "print-time-zone" (sh "date '+%Z'") (c: [
c.time-zone
])
)
|]
`shouldOutput` "UTC\n"
describe "try-readonly" $ do
it "binds paths as readonly in the jail" $ do
tmpDir <- getTestDir