Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat empty NIX_STORE_DIR env vars as unset #7918

Merged
merged 4 commits into from
Mar 3, 2023

Conversation

zimbatm
Copy link
Member

@zimbatm zimbatm commented Feb 28, 2023

Motivation

Fixes #6409

Actually written by @NinjaTrappeur

Context

See the above issue ^

We might want to expand the same change to the other env vars, IFF having empty env vars doesn't make sense to them.

Checklist for maintainers

Maintainers: tick if completed or explain if not relevant

  • agreed on idea
  • agreed on implementation strategy
  • tests, as appropriate
    • functional tests - tests/**.sh
    • unit tests - src/*/tests
    • integration tests - tests/nixos/*
  • documentation in the manual
  • code and comments are self-explanatory
  • commit message explains why the change was made
  • new feature or incompatible change: updated release notes

if (!value) return {};
auto str = std::string(value);
return !str.empty() ? std::optional<std::string>{str} : std::nullopt;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this could be expressed in terms of getEnv()? I.e. something like

    auto value = getEnv(key);
    return value == "" ? std::nullopt : value;

@Ericson2314
Copy link
Member

This feels ad-hoc to me. What about --store '' for example? This seems like giving into postal's law making errors go away, but the hidden cost is more complexity and more possibility for issues to hide.

The other issue had a bad error message. I would simply make a nice error message on the store URL parse to reject the empty string.

@Ericson2314
Copy link
Member

Oh the title is wrong this is about NIX_STORE_DIR; still we can make nicer error handling rather than silent accepting a dubious environment variables which problem indicate an issue elsewhere.

@zimbatm zimbatm changed the title treat empty NIX_STORE env vars as unset treat empty NIX_STORE_DIR env vars as unset Mar 1, 2023
@zimbatm
Copy link
Member Author

zimbatm commented Mar 1, 2023

@Ericson2314 I agree with your assessment in general, it's better to report failures than to paper over them. Bor env vars specifically, I think most scripts already don't make the distinction between empty and unset env vars ([ -z "${MY_ENV:-}" ]). It's probably asking for trouble if we rely on that distinction.

@Ericson2314
Copy link
Member

So I wouldn't mind if we did this for every environment variable --- at least it would be consistent. But for some of them it does matter, e.g. for NIX_PATH an empty one clears the default value.

@picnoir
Copy link
Member

picnoir commented Mar 1, 2023

👍 Makes sense.

But for some of them it does matter, e.g. for NIX_PATH an empty one clears the default value.

I think this pattern mostly makes sense for the env variables sent to canonPath where we're 100% sure that a empty value will result in a runtime failure. In that case, it'd probably be better to rather return a nullopt upstream (+ a warning) to move on with a default value.

@Ericson2314
Copy link
Member

Yeah I think it could be good to make a function that combines canonPath and getEnv use it everywhere possible, and then separately we can debate whether that function has a nice error or nice warning :)

We make sure the env var paths are actually set (ie. not "") before
sending them to the canonicalization function. If we forget to do so,
the user will end up facing a puzzled failed assertion internal error.

We issue a non-failing warning as a stop-gap measure. We could want to
revisit this to issue a detailed failing error message in the future.
@zimbatm
Copy link
Member Author

zimbatm commented Mar 1, 2023

^ is that better?

@Ericson2314
Copy link
Member

Yeah it is better :)

src/libutil/util.cc Outdated Show resolved Hide resolved
Co-authored-by: Théophane Hufschmitt <[email protected]>
auto value = getEnv(key);
if (value == "") {
// TODO: determine whether this should be a warning or an error.
warn("ignoring the '%1%' env variable, its value has been set to \"\"", key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop this warning. If empty vars are equivalent to the var not being set, there is no reason to complain about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not equal on my system at least:

philip@shiki ~> env FOO="" printenv FOO

philip@shiki ~> env printenv FOO
philip@shiki ~ [1]>

one returns "" and the other status 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I had something set to "", I’d be kind of confused if nix ignored it silently.

@zimbatm
Copy link
Member Author

zimbatm commented Mar 3, 2023

Anything else before I squash the commits?

@picnoir
Copy link
Member

picnoir commented Mar 3, 2023

I personally think that silently ignoring configuration paths that have been explicitly set by the user will make debugging such a situation much harder.

An env variable that is defined, but empty is likely a code smell/downstream bug, I think warning the user about that makes sense.

[Edit]: this post is apparently unclear, apologies. I personally think we should either warn the user and move on with the default value, or fail and throw an error.

@edolstra edolstra merged commit 0507462 into NixOS:master Mar 3, 2023
@Ericson2314
Copy link
Member

I agree with @NinjaTrappeur that the warning is important for debugging, which is why I asked for it. @edolstra would you be OK if we added the warning after all?

I would prefer the error, but I accept the warning as a middle ground.

@zimbatm zimbatm deleted the fix-empty-nix-store-env branch March 3, 2023 16:15
@Ericson2314
Copy link
Member

@NinjaTrappeur Can you open a new PR for the warning? That will give us something to discuss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nix profile upgrade '.*' fails
6 participants