nix-darwin
nix-darwin copied to clipboard
path in flake evaluated by darwin-rebuild is wrong
Description
A path used in a flake evaluates to "/private/var/root" resulting in a "No such file or directory" error. However, the same thing works fine in nix repl-flake repl.
Steps to Reproduce
- Using
nix --extra-experimental-features repl-flake repl, these work:
nix-repl> builtins.readFile (builtins.getEnv "HOME" + "/.config/hedonicadapter/colors.json")
"{\n \"base00\": \"#eeeeee\",\n \"base01\": \"#af0000\",\n \"base02\": \"#008700\",\n \"base03\": \"#5f8700\",\n \"base04\": \"#0087af\",\n \"base05\": \"#444444\",\n \"base06\": \"#005f87\",\n \"base07\": \"#878787\",\n \"base08\": \"#bcbcbc\",\n \"base09\": \"#d70000\",\n \"base0A\": \"#d70087\",\n \"base0B\": \"#8700af\",\n \"base0C\": \"#d75f00\",\n \"base0D\": \"#d75f00\",\n \"base0E\": \"#005faf\",\n \"base0F\": \"#005f87\"\n}\n"
nix-repl> builtins.readFile ~/.config/hedonicadapter/colors.json
"{\n \"base00\": \"#eeeeee\",\n \"base01\": \"#af0000\",\n \"base02\": \"#008700\",\n \"base03\": \"#5f8700\",\n \"base04\": \"#0087af\",\n \"base05\": \"#444444\",\n \"base06\": \"#005f87\",\n \"base07\": \"#878787\",\n \"base08\": \"#bcbcbc\",\n \"base09\": \"#d70000\",\n \"base0A\": \"#d70087\",\n \"base0B\": \"#8700af\",\n \"base0C\": \"#d75f00\",\n \"base0D\": \"#d75f00\",\n \"base0E\": \"#005faf\",\n \"base0F\": \"#005f87\"\n}\n"
- When used in a flake, however...
{
description = "Global color settings and utils";
outputs = {
nixpkgs,
...
}: let
colorsPath = builtins.toPath (builtins.getEnv "HOME" + "/.config/hedonicadapter/colors.json");
colors = builtins.fromJSON (builtins.readFile colorsPath);
OR
colorsPath = ~/.config/hedonicadapter/colors.json;
colors = builtins.fromJSON (builtins.readFile colorsPath);
...
{
description = "Nix-darwin config";
inputs = {
colors-flake.url = "github:hedonicadapter/colors-flake";
...
...both attempts fail with sudo darwin-rebuild switch --flake showing the wrong path:
error: opening file '/private/var/root/.config/hedonicadapter/colors.json': No such file or directory
I would not recommend using builtins.getEnv ever as that is quite impure. Instead you should copy the file you want to read into the same directory as your flake and then use a relative path reference like ./hedonicadapter/colors.json