# Nix-env : how i can install two differents versions of the same software inside a user

**URL:** https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490
**Category:** Help
**Created:** [July 17, 2019, 5:16pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490 "2019-07-17T17:16:26Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 17, 2019, 5:16pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/1 "2019-07-17T17:16:26Z")

</div>

Hello,

I want install in the same user two version of the software.

I think than specify the --profile directory was the solution.  
but this command 🙂  
nix-env -iA nixos.postgresql\_10 --profile ~/app/binary/  
retrun: ☹  
error: ‘/home/postgres/app/binary’ is not a symlink

So my question is :  
How i can install two version of the software binary?

Why i want to do this ?  
To use the pg\_upgrade binary to migrate db from one version to another.

Pierre

---

<div class="post-metadata">

### Author: ![Infinisil](https://discourse.nixos.org/user_avatar/discourse.nixos.org/infinisil/32/26523_2.png) [@Infinisil](https://discourse.nixos.org/u/Infinisil)
#### Post date: [July 17, 2019, 5:28pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/2 "2019-07-17T17:28:21Z")

</div>

It doesn’t really make sense to install multiple versions of packages, because you can’t have more than one version of a binary in your `PATH`.

I assume you intended to call `pg_upgrade` after installing like `~/app/binary/bin/pg_upgrade`? I recommend either calling the binary directly (which doesn’t install anything):

```bash
$(nix-build --no-out-link '<nixpkgs>' -A postgresql_10)/bin/pg_upgrade

```

Or running the binary in a `nix-shell`, which also doesn’t install anything:

```bash
nix-shell -p postgresql_10
[nix-shell:~]$ pg_upgrade

```

---

<div class="post-metadata">

### Author: ![lheckemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lheckemann/32/415_2.png) [@lheckemann](https://discourse.nixos.org/u/lheckemann)
#### Post date: [July 17, 2019, 6:05pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/3 "2019-07-17T18:05:33Z")

</div>

For completeness’s sake: you can create another profile, but it mustn’t already exist as a directory or file:

```auto
$ mkdir profile1
$ nix-env -p profile1 -iA nixos.hello
error: '/home/linus/profile1' is not a symlink
$ nix-env -p profile2 -iA nixos.hello
installing 'hello-2.10'

```

Note that this may not produce your desired behaviour though, for several reasons:

- It will not create garbage collection roots for the profile, meaning that its dependencies may be removed from under your feet during a garbage collection;
- It will keep historical versions of the profile right next to it. For example, the successful invocation of nix-env in my example created `profile2-1-link` in addition to `profile2`;
- The profile’s `bin` directory will not be in your `PATH`, nor will any other relevant environment variables be set, so you should have another use for putting them in a profile.

The first two issues can be solved by first creating a symlink to `/nix/var/nix/profiles/per-user/$USER/profilename`: `ln -s /nix/var/nix/profiles/per-user/$USER/testprofile profile3 && nix-env -p profile3 -iA nixos.hello`.

---

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 17, 2019, 6:09pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/4 "2019-07-17T18:09:08Z")

</div>

Hi Infinisil 😉 thank for your reply.

I sorry , but the command pg\_upgrade have need of a path on the two versions.  
pg\_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin

On Debian and all others distributions you can install several version.

You can have need to manage a cluster DB in 9.6 and one in 11.

Pierre

---

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 17, 2019, 6:27pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/5 "2019-07-17T18:27:46Z")

</div>

Hi Linus,

In case of postgres database software, we don’t install the same version.

We need to have the path on the oldversion and the newversion.

I search how resolve this problem inside nixos.

Nix-shell seem a way.  
Thank.  
Pierre

---

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 17, 2019, 7:01pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/6 "2019-07-17T19:01:40Z")

</div>

Hi  
It’s OK

I install postgresql 10 default profile

i launch a nix-shell with -p postgresql\_11

and create a link .  
ln -s /nix/store/88yxsbssvzrv22wymwy7m7ghpxy2fgnj-postgresql-11.4 pg11

after several step …  
launch the migrate from 10 to 11. 🙂

> ./app/binary/pg11/bin/pg\_upgrade -d ~/app/data/DB1 -D ~/app/data/DB2 -b /home/postgres/.nix-profile/bin -B ./app/binary/pg11/bin --link  
> Performing Consistency Checks

* * *

Checking cluster versions ok  
Checking database user is the install user ok  
Checking database connection settings ok  
Checking for prepared transactions ok  
Checking for reg\* data types in user tables ok  
Checking for contrib/isn with bigint-passing mismatch ok  
Creating dump of global objects ok  
Creating dump of database schemas  
ok  
Checking for presence of required libraries ok  
Checking database user is the install user ok  
Checking for prepared transactions ok

If pg\_upgrade fails after this point, you must re-initdb the  
new cluster before continuing.

## Performing Upgrade

Analyzing all rows in the new cluster ok  
Freezing all rows in the new cluster ok  
Deleting files from new pg\_xact ok  
Copying old pg\_xact to new server ok  
Setting next transaction ID and epoch for new cluster ok  
Deleting files from new pg\_multixact/offsets ok  
Copying old pg\_multixact/offsets to new server ok  
Deleting files from new pg\_multixact/members ok  
Copying old pg\_multixact/members to new server ok  
Setting next multixact ID and offset for new cluster ok  
Resetting WAL archives ok  
Setting frozenxid and minmxid counters in new cluster ok  
Restoring global objects in the new cluster ok  
Restoring database schemas in the new cluster  
ok  
Adding “.old” suffix to old global/pg\_control ok

If you want to start the old cluster, you will need to remove  
the “.old” suffix from /home/postgres/app/data/DB1/global/pg\_control.old.  
Because “link” mode was used, the old cluster cannot be safely  
started once the new cluster has been started.

Linking user relation files  
ok  
Setting next OID for new cluster ok  
Sync data directory to disk ok  
Creating script to analyze new cluster ok  
Creating script to delete old cluster ok

## Upgrade Complete

Optimizer statistics are not transferred by pg\_upgrade so,  
once you start the new server, consider running:  
./analyze\_new\_cluster.sh

Running this script will delete the old cluster’s data files:  
./delete\_old\_cluster.sh

Thank  
Pierre

---

<div class="post-metadata">

### Author: ![lilyball](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lilyball/32/939_2.png) [@lilyball](https://discourse.nixos.org/u/lilyball)
#### Post date: [July 17, 2019, 8:13pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/7 "2019-07-17T20:13:17Z")

</div>

If you want to run a binary from a given package it’s generally simpler to write this like `nix run nixpkgs.postgresql_10 -c pg_upgrade`.

---

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 18, 2019, 1:08pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/8 "2019-07-18T13:08:25Z")

</div>

Hello lilyball,

Thank about your reply.  
but the pg\_upgrade command need several args as you see below

> [@pirboazo](#):
>
> ./app/binary/pg11/bin/pg\_upgrade -d ~/app/data/DB1 -D ~/app/data/DB2 -b /home/postgres/.nix-profile/bin -B ./app/binary/pg11/bin --link

to bypass I created a symbolic link on the binary of the postgresql11 version.  
But it’s not very clean.

Isn’t it possible to create it with a nix command?

Pierre

---

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 18, 2019, 3:05pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/9 "2019-07-18T15:05:32Z")

</div>

Hi all

Creating links like these is not a solution  
because the next system cleanup will remove the packages in the /nix/store

How i can create profile on these links ?

Thank.  
Pierre

---

<div class="post-metadata">

### Author: ![lilyball](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lilyball/32/939_2.png) [@lilyball](https://discourse.nixos.org/u/lilyball)
#### Post date: [July 18, 2019, 6:30pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/10 "2019-07-18T18:30:18Z")

</div>

You can pass additional args to the `nix run` command. That said, the `-b /home/postgres/.nix-profile/bin` has me a little concerned, as `nix run` doesn’t touch that symlink.

---

<div class="post-metadata">

### Author: ![danbst](https://discourse.nixos.org/user_avatar/discourse.nixos.org/danbst/32/83_2.png) [@danbst](https://discourse.nixos.org/u/danbst)
#### Post date: [July 18, 2019, 9:45pm UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/11 "2019-07-18T21:45:48Z")

</div>

You don’t have to “install” things:

```auto
$(nix-build '<nixpkgs>' -A postgresql_11)/bin/pg_upgrade --link \
  -d ~/app/data/DB1 -D ~/app/data/DB2 \
  -b $(nix-build '<nixpkgs>' -A postgresql_10)/bin \
  -B $(nix-build '<nixpkgs>' -A postgresql_11)/bin

```

---

<div class="post-metadata">

### Author: ![lheckemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lheckemann/32/415_2.png) [@lheckemann](https://discourse.nixos.org/u/lheckemann)
#### Post date: [July 19, 2019, 5:17am UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/12 "2019-07-19T05:17:04Z")

</div>

```auto
nix-build '<nixpkgs>' -A postgresql_11 -o pg11
nix-build '<nixpkgs>' -A postgresql_10 -o pg10

```

Will create symlinks to the packages which are also GC roots, i.e. their dependencies won’t get removed by the garbage collector until the symlinks themselves are removed (with the caveat that the “removal detection” is fairly naïve and will consider the symlink removed if it is moved/renamed as well).

---

<div class="post-metadata">

### Author: ![pirboazo](https://discourse.nixos.org/user_avatar/discourse.nixos.org/pirboazo/32/562_2.png) [@pirboazo](https://discourse.nixos.org/u/pirboazo)
#### Post date: [July 19, 2019, 9:48am UTC](https://discourse.nixos.org/t/nix-env-how-i-can-install-two-differents-versions-of-the-same-software-inside-a-user/3490/13 "2019-07-19T09:48:01Z")

</div>

Hi Linus,

Your solution work fine 👍👍👍

Thank a lot.  
Pierre
