## Install Mathematica on Nixos

The `mathematica`

derivation is already in nixpkgs but is not self contained, and requires the user to manually add the installer to the nix-store. Therefore, i would recommend an imperative install. This does however mean that Mathematica will not persist, if you move your config to another machine.

### Imperative Install

Let’s try to imperatively install Mathematica like any other program:

```
nix-env -iA nixos.mathematica
```

This will probably result in an error like this:

```
Package ‘mathematica-13.2.1’ in /nix/var/nix/profiles/per-user/root/channels/nixos/pkgs/applications/science/math/mathematica/default.nix:76 has an unfree license (‘unfree’), refusing to evaluate.
```

The error warns us that we are trying to install a non-free (meaning not open-source) program. If you already purchased Mathematica, you probably do not care about this. To allow unfree packages in your current shell session run the following command:

```
export NIXPKGS_ALLOW_UNFREE=1
```

There should be no output.

After this we can try to install Mathematica again:

```
nix-env -iA nixos.mathematica
```

You will now get another error:

```
>***
> This nix expression requires that Mathematica_13.2.1_BNDL_LINUX.sh is
> already part of the store. Find the file on your Mathematica CD
> and add it to the nix store with nix-store --add-fixed sha256 <FILE>.
>
>***
```

This is because the derivation depends on proprietary Mathematica installer. You can download the installer from the Wolfram website here (of course you have to have a license).

After downloading the installer you may realize that your version is different from the version expected by the derivation. DO NOT PANIC. This is fine.

Add the downloaded installer to the nix-store with this command (substitute the “XX.X.X” with your version):

```
nix-store --query --hash \
$(nix store add-path Mathematica_XX.X.X_BNDL_LINUX.sh --name 'Mathematica_XX.X.X_BNDL_LINUX.sh')
```

This will take a while, as you are copying a ~5GB file to the nix-store. Just wait for it.

When done the command will output the hash for the added installer. Hold on to it.

If your version matches the version expected by the official derivation you should be able to run the install command without errors not:

```
nix-env -iA nixos.mathematica
```

Otherwise we will have to override the derivation ourselves.

#### Overriding the Default Version

Create a folder and add a file called `default.nix`

with the following contents:

```
{pkgs ? import <nixpkgs> { }}:
pkgs.mathematica.override {
source = pkgs.requireFile {
name = "Mathematica_XX.X.X_BNDL_LINUX.sh";
sha256 = "<your-hash>";
message = ''
Your override for Mathematica includes a different src for the installer,
and it is missing.
'';
hashMode = "recursive";
};
}
```

Fill in your version number in the `name`

field, and put your hash from before in the `sha256`

field.

If done correctly, you can now install mathematica by running this command in the folder:

```
nix-build
```

This will create a symlink called `result`

. Mathematica is now installed in the path `result/bin/mathematica`

.

## Desktop Icon

A derivation for adding a desktop icon may look something like this:

```
{ makeDesktopItem }:
makeDesktopItem {
name = "mathematica";
desktopName = "Mathematica";
exec = "<path-to-mathematica>";
icon = builtins.fetchurl {
url = "https://eits.uga.edu/_resources/files/images/mathematica-11-spikey.png";
sha256 = "sha256:1m0m20n26lyicnrjnzm6sng5w431lg64cqrzv0lwawkgv7dq4w0k";
};
}
```

* NOTE:* If you add this to your permanent configuration, you will end have a Mathematica desktop icon, also when Mathematica is not installed.