WS-KCNHUB/machines/kcnhub/packages/matlab.nix

154 lines
4.6 KiB
Nix

{ config, lib, pkgs, nix-matlab, ... }: let
# The matlab installation is held in the MATLAB_INSTALL_DIR MATLAB
# updates and toolboxes can primarily be handled with the matlab_mpm
# tool defined below. This file, alongside the matlab_install.txt
# describe the current version of MATLAB available on the
# system. Nix *can* fetch MATLAB and all its toolboxes, however not
# all MATLAB toolboxes can be installed via commands, some *must* be
# installed graphically, and otherwise are unsupported. So, for now,
# we try to make installation as documented and reproducible as
# possible.
MATLAB_INSTALL_DIR = "/opt/MATLAB";
# MATLAB Package Manager: MPM
#
# This tool is used to guarantee (nearly)
# reproducible installations of MATLAB, and to easily allow for
# controlling what version of MATLAB is currently installed
matlab-mpm = pkgs.stdenv.mkDerivation rec {
pname = "matlab-package-manager";
version = "2023.12.1";
src = pkgs.fetchurl {
url = "https://ssd.mathworks.com/supportfiles/downloads/mpm/${version}/glnxa64/mpm";
hash = "sha256-NlfPLDnpRIaKLLuIZAudnFHBKL04gzmypzp98QOa6+g=";
};
nativeBuildInputs = with pkgs; [
autoPatchelfHook
];
buildInputs = with pkgs; [
gawk
gnugrep
unzip
linux-pam
libz
];
unpackPhase = "cp $src ./mpm";
buildPhase = ''
tail -n +$(head -n20 ./mpm | grep "tail -n" | awk '{print $3}' | grep -o -E "[0-9]+") ./mpm > ./mpm.zip
'';
installPhase = ''
unzip -d $out ./mpm.zip
'';
passthru = {
executable = "bin/glnxa64/mpm";
};
};
# Environment with the dependencies needed to run matlab (and
# matlab's installers)
matlab-env = pkgs.buildFHSUserEnv {
name = "matlab-env";
targetPkgs = (ps: (nix-matlab.targetPkgs ps) ++ (with ps; [
linux-pam
]));
};
# Script used to get the most up to date version of the desired MATLAB
matlab-update-script = pkgs.writeScriptBin "matlab_update_installation.sh" ''
#!${pkgs.bash}/bin/bash
if [ "''${EUID:-$(id -u)}" -ne 0 ]; then
echo "Please make sure to run this script as root"
exit -1
fi
if [ ! -d "${MATLAB_INSTALL_DIR}" ]; then
echo "Make sure that '${MATLAB_INSTALL_DIR}' exists"
exit -1
fi
cd ${MATLAB_INSTALL_DIR}
cp ${./matlab/install_file.txt} ./install_file.txt
sed -e "s#@MATLAB_INSTALL_DIR@#${MATLAB_INSTALL_DIR}#" -i ./install_file.txt
${matlab-env}/bin/${matlab-env.name} -c '${matlab-mpm}/${matlab-mpm.executable} install --inputfile ./install_file.txt'
'';
desktopItem = pkgs.makeDesktopItem {
desktopName = "Matlab";
name = "matlab";
# We use substituteInPlace after we run `install`
# -desktop is needed, see:
# https://www.mathworks.com/matlabcentral/answers/20-how-do-i-make-a-desktop-launcher-for-matlab-in-linux#answer_25
exec = "@out@/bin/matlab -desktop %F";
icon = "matlab";
# Most of the following are copied from octave's desktop launcher
categories = [
"Utility"
"TextEditor"
"Development"
"IDE"
];
mimeTypes = [
"text/x-octave"
"text/x-matlab"
];
keywords = [
"science"
"math"
"matrix"
"numerical computation"
"plotting"
];
};
# Produce acessible executables to "f", e.g. "matlab" or "mex"
matlab-run = f: pkgs.buildFHSUserEnv {
name = f;
targetPkgs = nix-matlab.targetPkgs;
# If making MATLAB available, create Application Icon etc.
extraInstallCommands = pkgs.lib.strings.optionalString (f == "matlab") ''
install -Dm644 ${desktopItem}/share/applications/matlab.desktop $out/share/applications/matlab.desktop
substituteInPlace $out/share/applications/matlab.desktop \
--replace "@out@" ${placeholder "out"}
install -Dm644 ${nix-matlab}/icons/hicolor/256x256/matlab.png $out/share/icons/hicolor/256x256/matlab.png
install -Dm644 ${nix-matlab}/icons/hicolor/512x512/matlab.png $out/share/icons/hicolor/512x512/matlab.png
install -Dm644 ${nix-matlab}/icons/hicolor/64x64/matlab.png $out/share/icons/hicolor/64x64/matlab.png
'';
runScript = pkgs.writeScript "matlab-run-${f}-script" ''
export QT_QPA_PLATFORM=xcb
"$MATLAB_INSTALL_DIR/bin/${f}" "$@"
'';
};
in {
environment.sessionVariables = {
inherit MATLAB_INSTALL_DIR;
};
nixpkgs.overlays = [
(final: prev: {
inherit matlab-update-script;
matlab = (matlab-run "matlab");
matlab-mex = (matlab-run "mex");
})
];
environment.systemPackages =(with pkgs; [
matlab-update-script
matlab
matlab-env
matlab-mex
octaveFull
]);
}