Is it possible to change the name of a project environment after having created it?

I couldn’t find a way to do except by forcing my way into .flox. Is there a proper way to do it?

Howdy, this is a good question I don’t know myself but I’ll ask the engineers.

1 Like

What was recommended was to use flox export in combination with flox import, then flox destroy on the original.

An important caveat here is that you’ll lost the “history” associated with that environment.

Something like:

$ flox create -e foo;
created environment foo (x86_64-linux)

$ flox install -e foo jq;
Installed 'jq' package(s) into 'foo' environment.

$ flox export -e foo|flox import -e bar;
Environment 'bar' imported.

$ flox destroy -e foo;                  
WARNING: you are about to delete the following:
 - /home/USER/.local/share/flox/environments/USER/x86_64-linux.foo
 - /home/USER/.local/share/flox/environments/USER/x86_64-linux.foo-1-link
 - the x86_64-linux.foo branch in /home/USER/.cache/flox/meta/USER
Are you sure? (y/N) y
Deleted branch x86_64-linux.foo (was 5bafe4c).
removed '/home/USER/.local/share/flox/environments/USER/x86_64-linux.foo'
removed '/home/USER/.local/share/flox/environments/USER/x86_64-linux.foo-1-link'

Thanks Alex. I was however specifically asking this about project environments for which export and import are not supported. I am asking this because I have, at work, several copies of the main repository, work-1, work-2,… and when I do flox init I get a project environment called work-1. But it makes more sense to commit it as just work when adding flox files to the repository.

Gotcha. Let me ask the team and get back to you.

To rename a project env I believe you can simply move the folder under ./pkgs/foo -> ./pkgs/bar:

$ mkdir /tmp/rename;
$ cd /tmp/rename;
$ flox init -t project -i;
$ flox init -t package-simple -n foo;
$ flox list;
Select package for flox list
  default
> foo
HINT: avoid selecting a package next time with:
$ flox list -e .#foo
/tmp/rename#foo
    Alias     .#foo
    System    x86_64-linux
    Path      /tmp/rename/.flox/envs/x86_64-linux.foo

Packages
$ mv ./pkgs/foo ./pkgs/bar;
$ git add ./pkgs/bar;
$ flox list;
Select package for flox list
  default
> bar
HINT: avoid selecting a package next time with:
$ flox list -e .#bar
/tmp/rename#bar
    Alias     .#bar
    System    x86_64-linux
    Path      /tmp/rename/.flox/envs/x86_64-linux.bar

Can you please try installing packages to the foo environment before doing the renaming? If I do this I don’t see installed packages after the renaming.

If you could provide a snippet to reproduce I could help debug.

This seems to work for me, maybe it will help unblock you:

$ mkdir /tmp/foo;
$ cd /foo;
$ flox init -t project -i;
$ flox init -t package-simple -n foo;

# Manually create an env for this package
$ cat <<'EOF' > ./pkgs/foo/flox.nix
{
    environmentVariables.envName = baseNameOf ./.;
  shell.hook = ''
    echo "Welcome to the '$envName' environment" >&2;
  '';
}
EOF

# Re-render the changed environment
$ EDITOR=cat flox edit '.#foo';
$ bash -c ". <( flox activate -e '.#foo'; );" 2>/dev/null;
Welcome to the 'foo' environment

# Add a package
$ flox install -e '.#foo' hello;
Installed 'hello' package(s) into '.#foo' environment.

# Rename the env
$ mv ./pkgs/foo ./pkgs/bar;
$ git add ./pkgs/bar;

# Render the new env
$ EDITOR=cat flox edit '.#bar';
$ bash -c ". <( flox activate -e '.#bar'; );" 2>/dev/null;
Welcome to the 'bar' environment

The critical piece here is the “reload”/“re-render”.

You may also find this direnv snippet useful, which makes it so you don’t need to manually re-render:

# Reload any changes to `flox.nix' file.
# Capture output and check for errors.
_msg="$( EDITOR=cat flox edit -e '.#default' 2>&1; )";
case "$_msg" in
  *ERROR*|*error*)
    echo "Failed to update flox environment:" >&2;
    echo "$_msg" >&2;
    exit 1;
  ;;
  *)
    # Activate `flox' env.
    . <( flox activate -e '.#default'; );
  ;;
esac

Here is a session showing the issue:

> cd /tmp
> mkdir /foo
> flox init
> flox activate
> flox install exa
> flox list
/private/tmp/foo#foo
    Alias     .#foo
    System    aarch64-darwin
    Path      /private/tmp/foo/.flox/envs/aarch64-darwin.foo

Packages
    0  stable.nixpkgs-flox.exa  0.10.1

> mv ./pkgs/foo ./pkgs/bar
> <exit>
> flox activate
> flox list                                                                                                                                                                                                                                                                                                                                                                                                                         etorreborre/aarch64-darwin.default
    Alias     default
    System    aarch64-darwin
    Path      /Users/etorreborre/.local/share/flox/environments/etorreborre/aarch64-darwin.default
    Curr Gen  64

Packages
    0   stable.etorreborre.cheat-sh@1.0.0       1.0.0
    1   stable.etorreborre.commit-tool@0.7.1.1  cmt-0.7.1.1
> git add .
> git commit
> flox activate
> flox list
/private/tmp/foo#bar
    Alias     .#bar
    System    aarch64-darwin
    Path      /private/tmp/foo/.flox/envs/aarch64-darwin.bar

Packages
<no packages>

And indeed if I do

> EDITOR=cat flox edit -e '.#bar';
> flox list
/private/tmp/foo#bar
    Alias     .#bar
    System    aarch64-darwin
    Path      /private/tmp/foo/.flox/envs/aarch64-darwin.bar

Packages
    0  stable.nixpkgs-flox.exa  0.10.1

But you have to admit that having to re-render the environment is not an obvious step :-). That’s why maybe a rename command could be a good idea.

Question about the direnv snippet: I should change #default with #foo for my foo project` right?

Completely agree that this is a wonky UX issue that needs attention, we’ve been discussing improvements since we had a few questions similar to this one.

And yes you can change the direnv snippet to use any environment in your project. In that example I just chose the default.

Another note is that I was told EDITOR=true flox edit; also works. The only difference is that it doesn’t print out your flox.nix file which may be more desirable in interactive mode.

1 Like