diff --git a/lib/combinators/time-zone.nix b/lib/combinators/time-zone.nix index b79b58e..eaa661d 100644 --- a/lib/combinators/time-zone.nix +++ b/lib/combinators/time-zone.nix @@ -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 + ''); } diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 2667dc0..46e165c 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -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