From 45c2b47d7130bddd3e7f16441e94cb2237d4ffa4 Mon Sep 17 00:00:00 2001 From: Alex David Date: Mon, 15 Sep 2025 21:57:18 -0700 Subject: [PATCH] Fix time-zone combinator Most software that wants to know the timezone reads the symlink in `/etc/localtime`. A previous update broke this assumption when attempting to fix the combinator to work on non-NixOS systems. This change should fix the time-zone combinator in both NixOS and non-NixOS systems. --- .build.yml | 1 + lib/combinators.nix | 7 +++++- tests/spec/CombinatorsSpec.hs | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/.build.yml b/.build.yml index ac9aefd..2282159 100644 --- a/.build.yml +++ b/.build.yml @@ -2,6 +2,7 @@ image: nixos/25.05 environment: NIX_CONFIG: "experimental-features = nix-command flakes" tasks: + - setup: "sudo ln -s /etc/zoneinfo/EST /etc/localtime" - test-jail-nix: "cd jail.nix/tests && nix run .#runTests" - check-format: "cd jail.nix/tests && nix run .#checkFmt" - build-website: "cd jail.nix/website && nix build -L .#default" diff --git a/lib/combinators.nix b/lib/combinators.nix index d76edac..3a9dee0 100644 --- a/lib/combinators.nix +++ b/lib/combinators.nix @@ -860,7 +860,12 @@ rec { doc = '' Exposes your timezone. ''; - __functor = _: include-once "time-zone" (runtime-deep-ro-bind "/etc/localtime"); + __functor = + _: + 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") + ]); }; network = { diff --git a/tests/spec/CombinatorsSpec.hs b/tests/spec/CombinatorsSpec.hs index 32cf99c..7708310 100644 --- a/tests/spec/CombinatorsSpec.hs +++ b/tests/spec/CombinatorsSpec.hs @@ -194,3 +194,47 @@ spec = parallel $ inTestM $ do ]) |] `shouldOutput` "some-value" + + describe "time-zone" $ do + it "forwards timezones on nixos systems" $ do + [i| + let + tz = "EST"; + 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: [ + # This is a bit convoluted, but this recreates the symlink chain that is present on nixos systems + (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") + (c.unsafe-add-raw-args "--symlink /etc/zoneinfo/${tz} /etc/localtime") + ]); + in + withFakeTimeZoneOnNixos ( + jail "print-time-zone" (sh "date '+%Z'") (c: [ + c.time-zone + ]) + ) + |] + `shouldOutput` "EST\n" + + it "forwards timezones on non-nixos systems" $ do + [i| + let + tz = "EST"; + withFakeTimeZoneOnNixos = program: + jail program.name program (c: [ + (c.unsafe-add-raw-args "--ro-bind ${pkgs.tzdata}/share/zoneinfo /etc/zoneinfo") + (c.unsafe-add-raw-args "--symlink /etc/zoneinfo/${tz} /etc/localtime") + ]); + in + withFakeTimeZoneOnNixos ( + jail "print-time-zone" (sh "date '+%Z'") (c: [ + c.time-zone + ]) + ) + |] + `shouldOutput` "EST\n"