<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://www.sharcnet.ca/help/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Roberpj</id>
		<title>Documentation - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://www.sharcnet.ca/help/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Roberpj"/>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php/Special:Contributions/Roberpj"/>
		<updated>2026-05-23T19:45:48Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17542</id>
		<title>LSDYNA</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17542"/>
				<updated>2019-02-08T20:16:55Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Command Line Use */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=LSDYNA&lt;br /&gt;
|package_description=Suite of programs for transient dynamic finite element program&lt;br /&gt;
|package_idnumber=104&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Before a research group can use LSDYNA on sharcnet, a license must be purchased directly from LSTC for the sharcnet license server.  Alternately, if a research group resides at a institution that has sharcnet computing hardware (mac, uwo, guelph, waterloo) it maybe possible to use a pre-existing site license hosted on an accessible institutional license server.   To access and use this software you must open a ticket and request to be added to the sharcnet &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt; group.&lt;br /&gt;
&lt;br /&gt;
=Version Selection=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Create and customize this file for your license server, where XXXXX should be replaced with your port number and Y with your server number:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra1:~] cat .licenses/ls-dyna.lic &lt;br /&gt;
 #LICENSE_TYPE: network&lt;br /&gt;
 #LICENSE_SERVER: XXXXX@licenseY.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The modules are loaded automatically in the testomp.sh and testmpi.sh script shown below in the example section:&lt;br /&gt;
&lt;br /&gt;
For single node smp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna&lt;br /&gt;
        ls-dyna/7.1.2&lt;br /&gt;
        ls-dyna/7.1.3&lt;br /&gt;
        ls-dyna/8.1&lt;br /&gt;
        ls-dyna/9.1&lt;br /&gt;
        ls-dyna/9.2&lt;br /&gt;
        ls-dyna/10.0&lt;br /&gt;
&lt;br /&gt;
To load any version do for example:&lt;br /&gt;
&lt;br /&gt;
 module load ls-dyna/9.1&lt;br /&gt;
&lt;br /&gt;
For multi node mpp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna-mpi&lt;br /&gt;
        ls-dyna-mpi/7.1.2&lt;br /&gt;
        ls-dyna-mpi/7.1.3&lt;br /&gt;
        ls-dyna-mpi/8.1&lt;br /&gt;
        ls-dyna-mpi/9.1&lt;br /&gt;
        ls-dyna-mpi/9.2&lt;br /&gt;
        ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
To load the 7.1.X modules do the following:&lt;br /&gt;
 module load openmpi/1.6.5 ls-dyna-mpi/7.1.3&lt;br /&gt;
&lt;br /&gt;
To load the 8.X or 9.X modules do:&lt;br /&gt;
 module load openmpi/1.8.8 ls-dyna-mpi/9.2&lt;br /&gt;
&lt;br /&gt;
To load the 10.X modules do:&lt;br /&gt;
 module load openmpi/1.10.7 ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Once a license configuration is established, the research group will be given a 5 digit port number.  The value should then be inserted into the appropriate departmental export statement before loading the module file as follows:&lt;br /&gt;
&lt;br /&gt;
o UofT Mechanical Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o McGill Mechanical Engineering Department&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license2.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o UW Mechanical and Mechatronics Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license3.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Laurentian University Bharti School of Engineering&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license4.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Fictitious Example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;12345&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The next step is load the desired sharcnet lsdyna module version.  Check which modules are available by running the &amp;lt;i&amp;gt;module avail&amp;lt;/i&amp;gt; command then load one the modules as shown:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] module avail lsdyna&lt;br /&gt;
------------------------------------ /opt/sharcnet/modules ------------------------------------&lt;br /&gt;
lsdyna/hyb/r711.88920     lsdyna/mpp/r711.88920     lsdyna/smp/r611.79036&lt;br /&gt;
lsdyna/mpp/ls971.r85718   lsdyna/mpp/r712.95028     lsdyna/smp/r711.88920&lt;br /&gt;
lsdyna/mpp/ls980B1.011113 lsdyna/mpp/r800.95359     lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna/mpp/r611.79036     lsdyna/mpp/r901.109912    lsdyna/smp/r800.95359&lt;br /&gt;
lsdyna/mpp/r611.80542     lsdyna/smp/ls980B1.78258  lsdyna/smp/r901.109912&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r9.0.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r901.109912&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r901.109912&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r8.0.0 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r800.95359&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r800.95359&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.2 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r712.95028&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/mpp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r6.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r611.79036&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
or&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.80542&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;i&amp;gt;r611.80542&amp;lt;/i&amp;gt; provides lsdyna_s only.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====ls980 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/ls980B1.78258&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.4.5 lsdyna/mpp/ls980B1.011113&lt;br /&gt;
or&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/smp/ls980B1.78258&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note1) restart capability for ls980smpB1 and ls980mppB1 is not supported&lt;br /&gt;
Note2) the module using legacy openmpi/intel/1.4.5 will run extremely slow&lt;br /&gt;
&lt;br /&gt;
=Job Submission=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
The submission scripts myompjob.sh and mympijob.sh for the airbag problem are shown in the &amp;lt;b&amp;gt;Example Job&amp;lt;/b&amp;gt; section below, for both graham and orca.  Please note that you should specify your own username if submitting to a def account (not roberpj).  Alternatively you could specify your resource allocation account:&lt;br /&gt;
&lt;br /&gt;
Sample threaded job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch myompjob.sh&lt;br /&gt;
&lt;br /&gt;
Sample mpi job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch  mympijob.sh&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
When loading a lsdyna sharcnet legacy module on the new orca or a sharcnet legacy system, the single or double precision solvers are specified with &amp;lt;strong&amp;gt;lsdyna_s&amp;lt;/strong&amp;gt; or &amp;lt;strong&amp;gt;lsdyna_d&amp;lt;/strong&amp;gt; respectively, as shown in the following sqsub commands:&lt;br /&gt;
&lt;br /&gt;
===1cpu SERIAL Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q serial -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===4cpu SMP Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=1G lsdyna_s i=airbag.deploy.k ncpu=4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using an explicit solver, one can specify a conservative initial memory setting on the command line as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=4 memory=754414&amp;lt;/pre&amp;gt;&lt;br /&gt;
where memory is the minimum number of 8 byte words shared by all processors in double precision, or, 4 byte words in single precision.&lt;br /&gt;
&lt;br /&gt;
The initial value can be determined by starting a simulation interactively on the command line, and finding the output resembling: &amp;lt;i&amp;gt; Memory required to begin solution      :      754414&amp;lt;/i&amp;gt;.    The number of words can be specified as memory=260M instead of memory=260000000,  for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  &lt;br /&gt;
&lt;br /&gt;
===8cpu MPI Job===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The initial memory can also be specified for mpi jobs  on the sqsub command line with &amp;quot;memory=&amp;quot; for the first master processor to decompose the problem  and &amp;quot;memory2=&amp;quot; used on all processors including the master to solves the decomposed problem, where the values are specified as 4 bytes per word in single precision and 8 bytes per word in double precision.   The number of words can be specified as memory=260M instead of memory=260000000 OR memory2=260M instead of memory2=260000000 for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  The initial values can be found by running simulation interactively on a orca compute node and checking the output for a line such as: &amp;lt;i&amp;gt;Memory required to begin solution (memory=     464898  memory2=     158794 )&amp;lt;/i&amp;gt; which could then be implemented for a job run in the queue by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898 memory2=158794&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The specification of memory2 on sharcnet or compute canada clusters is not beneficial since the queue reserves the same memory per core for all nodes, such that the decomposition master node process cannot be allocated to have larger system memory.  Therefore its sufficient to specify a single memory parameter by doing:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto &lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the LSTC_MEMORY variable will only allow the memory to grow for explicit simulations. The following slurm examples demonstrate how to prescribe the memory parameters exactly.&lt;br /&gt;
&lt;br /&gt;
=Example Job=&lt;br /&gt;
&lt;br /&gt;
Copy the airbag example to your account:&lt;br /&gt;
&lt;br /&gt;
 rsync -av orca.computecanada.ca:/opt/sharcnet/lsdyna/r901.109912/examples r901.109912_examples&lt;br /&gt;
 cd r901.109912_examples/examples/misc/airbag&lt;br /&gt;
 gunzip airbag.deploy.k.gz&lt;br /&gt;
&lt;br /&gt;
==Graham (default modules)==&lt;br /&gt;
&lt;br /&gt;
Please note that graham does not have the sharcnet legacy modules installed on it.&lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mysmpjob1.sh&amp;lt;/b&amp;gt; for 4 core single precision smp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=4                   # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testsmp1                 # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load ls-dyna/9.1&lt;br /&gt;
ls-dyna_s i=airbag.deploy.k ncpu=4 memory=1000M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 4 (bytes/word) = 1000M words&lt;br /&gt;
&lt;br /&gt;
===Mpi Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mympijob2.sh&amp;lt;/b&amp;gt; for 4 core double precision mpp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --ntasks=4                     # number of ranks&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M            # memory per rank&lt;br /&gt;
#SBATCH --time=00:30:00                # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testmpi2            # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load openmpi/1.8.8 ls-dyna-mpi/9.1&lt;br /&gt;
srun ls-dyna_d i=airbag.deploy.k memory=500M memory2=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
==New Orca (legacy modules)==&lt;br /&gt;
&lt;br /&gt;
Please note, while new orca has most compute canada cvmfs modules available by default it does not have the graham the &amp;lt;i&amp;gt;ls-dyna&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;ls-dyna-mpi&amp;lt;/i&amp;gt; modules installed.  Therefore currently the only way to run lsdyna is by using the legacy sharcnet &amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt; modules as shown in the following two smp and mpi examples. &lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Submission script &amp;lt;b&amp;gt;mysmpjob3.sh&amp;lt;/b&amp;gt; to run 16 core single precision single node smp job with sharcnet legacy modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=16                  # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=mysmpjob3-1node-16core    # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server for legacy modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=31047@license3.sharcnet.ca&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does NOT work with legacy modules&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna_s i=airbag.deploy.k ncpu=$SLURM_CPUS_PER_TASK memory=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
===MPI Job ===&lt;br /&gt;
&lt;br /&gt;
To run the sharcnet legacy &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; efficiently over several compute nodes with many cores requires using orca's xeon nodes as shown in &amp;lt;b&amp;gt; mympijob4.sh&amp;lt;/b&amp;gt; below.   To run lsdyna on orca's opteron nodes with &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; requires using only &amp;lt;b&amp;gt;one&amp;lt;/b&amp;gt; opteron node as shown in &amp;lt;b&amp;gt; mympijob5.sh&amp;lt;/b&amp;gt; below.  The other legacy sharcnet lsdyna mpp modules, as shown in the Availability Table  lsdyna/mpp/ls971.r85718, lsdyna/mpp/ls980B1.011113, lsdyna/mpp/r611.79036, lsdyna/mpp/r611.80542, lsdyna/mpp/r711.88920, lsdyna/mpp/r800.95359 and lsdyna/mpp/r901.109912 have yet to be tested. If you want to use one but cannot get it working please open a ticket.  Note that lines containing a space between &amp;quot;# and SBATCH&amp;quot; are comments.  To activate such line(s) required removal of the space ie) &amp;quot;#SBATCH&amp;quot;.  Its recommended to use &amp;lt;i&amp;gt;module ls-dyna-mpi&amp;lt;/i&amp;gt; on graham instead since the machine is far newer, faster and reliable. &lt;br /&gt;
&lt;br /&gt;
====Multi Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob4.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=xeon&lt;br /&gt;
#SBATCH --ntasks=48&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob4-Xnodes-48cores-xeon  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out             # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE --mca plm_slurm_args '--cpu_bind=none' lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob5.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=opteron&lt;br /&gt;
#SBATCH --nodes=1&lt;br /&gt;
# SBATCH --ntasks=24&lt;br /&gt;
# SBATCH --mem=0&lt;br /&gt;
#SBATCH --ntasks=16&lt;br /&gt;
#SBATCH --mem-per-cpu=1000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob5-1node-16cores-opteron  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 1000M / 4 (bytes/word) = 250M words&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
STEP1) The following shows sqsub submission of the [http://www.dynaexamples.com/examples-manual/misc/airbag airbag] example to the mpi queue.  Its recommended to first edit  &amp;lt;i&amp;gt;airbag.deploy.k&amp;lt;/i&amp;gt;  and change &amp;lt;i&amp;gt;endtim&amp;lt;/i&amp;gt; to 3.000E-00 so the job runs long enough to perform the restart in steps 2 and 3 below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp -a /opt/sharcnet/lsdyna/r611.79036/examples /scratch/$USER/test-lsdyna&lt;br /&gt;
cd /scratch/$USER/test-lsdyna/misc/airbag&lt;br /&gt;
gunzip airbag.deploy.k.gz&lt;br /&gt;
cp airbag.deploy.k airbag.deploy.restart.k&lt;br /&gt;
nano deploy.restart.k (reduce plot file creation frequency)&lt;br /&gt;
   *DATABASE_BINARY_D3PLOT&lt;br /&gt;
   $       dt      lcdtdeploy.restart.k&lt;br /&gt;
   1.000E-01   &amp;lt;--- change from 5.000E-04 &lt;br /&gt;
export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
sqsub -r 10m -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP2) With the job still running, use the echo command as follows to create a file called &amp;quot;D3KIL&amp;quot; that will trigger generation of restart files at which point the file D3KIL itself will be erased.  Do this once a day if data loss is critical to you OR once or twice just before the  &amp;lt;i&amp;gt;sqsub -r&amp;lt;/i&amp;gt; time limit is reached.  Further information can be found here http://www.dynasupport.com/tutorial/ls-dyna-users-guide/sense-switch-control:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
echo &amp;quot;sw3&amp;quot; &amp;gt; D3KIL&lt;br /&gt;
sqkill job#&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP3) Before the job can be restarted, the following two lines must be added to the &amp;lt;i&amp;gt;airbag.deploy.restart.k&amp;lt;/i&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*CHANGE_CURVE_DEFINITION&lt;br /&gt;
         1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP4) Now resubmit the job as follows using &amp;quot;r=&amp;quot; to specify the restart file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sqsub -r 1h -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.restart.k r=d3dump01&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=General Notes=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
There are no special notes at the present time in regards to using lsdyna on graham.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Command Line Use==&lt;br /&gt;
&lt;br /&gt;
Following the upgrade of orca to the compute canada module stack, the orca development nodes were discontinued.&amp;lt;br&amp;gt;&lt;br /&gt;
Therefore this section is no longer relevent and remain for historical purposes only.&lt;br /&gt;
&lt;br /&gt;
Parallel lsdyna jobs can be run interactively on the command line (outside the queue) for short run testing purposes as follows:&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node MPP (mpi) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/mpp/r711.88920&lt;br /&gt;
 mpirun -np 8 lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node SMP (threaded) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load lsdyna/smp/r711.88920&lt;br /&gt;
 lsdyna_d i=airbag.deploy.k ncpu=8&lt;br /&gt;
&lt;br /&gt;
==Memory Issues==&lt;br /&gt;
&lt;br /&gt;
A minumum of mpp=2G is recommended although the memory requirement of a job may suggest much less is required. For instance setting mpp=1G for the airbag test job above will result in the following error when running a job in the queue:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
An attempt to set processor affinity has failed - please check to&lt;br /&gt;
ensure that your system supports such functionality. If so, then&lt;br /&gt;
this is probably something that should be reported to the OMPI developers.&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another error message which can occur after a job runs for a while if mpp is chosen to small is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Process 45 &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Signal 11 : Segmentation Violation &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To get an idea of the amount of memory a job used run grep on the output file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep Memory&lt;br /&gt;
     |    Distributed Memory Parallel                  |&lt;br /&gt;
 Memory size from command line:      500000,       500000&lt;br /&gt;
 Memory for the head node&lt;br /&gt;
 Memory installed (MB)        :        32237&lt;br /&gt;
 Memory free (MB)             :        11259&lt;br /&gt;
 Memory required (MB)         :            0&lt;br /&gt;
 Memory required to process keyword     :       458120&lt;br /&gt;
 Memory required for decomposition      :       458120&lt;br /&gt;
 Memory required to begin solution (memory=      458120 memory2=      230721)&lt;br /&gt;
 Max. Memory reqd for implicit sol: max used               0&lt;br /&gt;
 Max. Memory reqd for implicit sol: incore                 0&lt;br /&gt;
 Max. Memory reqd for implicit sol: oocore                 0&lt;br /&gt;
 Memory required to complete solution (memory=      458120 memory2=      230721)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Version and Revision==&lt;br /&gt;
&lt;br /&gt;
Again run grep on the output file to extract the major and minor revision:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep 'Revision\|Version'&lt;br /&gt;
     |  Version : mpp s R6.1.1    Date: 01/02/2013     |&lt;br /&gt;
     |  Revision: 78769           Time: 07:43:30       |&lt;br /&gt;
     |  SVN Version: 80542                             |&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Check License Status==&lt;br /&gt;
&lt;br /&gt;
===Running and Queued Programs===&lt;br /&gt;
&lt;br /&gt;
To get a summary of running and/or queued jobs use the &amp;lt;i&amp;gt;lstc_qrun&amp;lt;/i&amp;gt; command as follows, where queued means the job has started running according to the sqsub command but its actually sitting waiting for license resources to come available.  Once these are acquired the job will start running on the cluster and appear as a Running Program according to  lstc_qrun ie)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
                     Running Programs&lt;br /&gt;
    User             Host          Program              Started       # procs&lt;br /&gt;
-----------------------------------------------------------------------------&lt;br /&gt;
dgierczy    20277@saw61.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dgierczy     8570@saw32.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dscronin    25486@hnd6             MPPDYNA_971      Tue Apr  9 20:19     6&lt;br /&gt;
dscronin    14897@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    14971@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    15046@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    31237@hnd16            MPPDYNA_971      Tue Apr  9 21:53     6&lt;br /&gt;
dscronin    31313@hnd16            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
dscronin     6396@hnd15            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
csharson    28890@saw175.saw.sharc MPPDYNA_971      Wed Apr 10 16:48     6&lt;br /&gt;
No programs queued&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To show license file expiration details, append &amp;quot;-R&amp;quot; to the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun -R&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
**** LICENSE INFORMATION ****&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-DYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dgierczy   20277@saw61.saw.sharcnet   1&lt;br /&gt;
 dgierczy    8570@saw32.saw.sharcnet   1&lt;br /&gt;
MPPDYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dscronin   25486@hnd6               6&lt;br /&gt;
 dscronin   14897@hnd18              6&lt;br /&gt;
 dscronin   14971@hnd18              6&lt;br /&gt;
 dscronin   15046@hnd18              6&lt;br /&gt;
 dscronin   31237@hnd16              6&lt;br /&gt;
 dscronin   31313@hnd16              6&lt;br /&gt;
 dscronin    6396@hnd15              6&lt;br /&gt;
 csharson   28890@saw175.saw.sharcnet   6&lt;br /&gt;
                   LICENSE GROUP    58    966   1024 |     0&lt;br /&gt;
&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-OPT           12/31/2013          0   1024   1024 |     0&lt;br /&gt;
                   LICENSE GROUP     0   1024   1024 |     0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Killling A Program===&lt;br /&gt;
&lt;br /&gt;
The queue normally kills lsdyna jobs cleanly.  However its possible that licences for a job (which is no longer running in the queue and therefore no longer has any processes running on the cluster) will continue to be tied up according to the lstc_qrun command.   To kill such a program determine the pid@hostname from the lstc_qrun command then run the following kill command.  The following example demonstrates the procedure for username roberpj:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
roberpj    45312@orc329.orca.shar MPPDYNA          Wed Dec  5 15:50    12&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~] lstc_qkill 45312@orc329.orca.sharcnet.ca&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
Program queued for termination&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
[roberpj@orc-login2:~] &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Legacy Instructions==&lt;br /&gt;
&lt;br /&gt;
The following binaries remain available on saw and orca for backward compatibility testing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] cd /opt/sharcnet/local/lsdyna&lt;br /&gt;
[roberpj@orc129:/opt/sharcnet/local/lsdyna] ls ls971*&lt;br /&gt;
ls971_d_R3_1    ls971_d_R4_2_1  ls971_s_R3_1    ls971_s_R4_2_1  ls971_s_R5_1_1&lt;br /&gt;
ls971_d_R4_2_0  ls971_d_R5_0    ls971_s_R4_2_0  ls971_s_R5_0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are currently no sharcnet modules for these versions, hence jobs should be submitted as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load lsdyna&lt;br /&gt;
export PATH=/opt/sharcnet/local/lsdyna:$PATH&lt;br /&gt;
export LSTC_LICENSE_SERVER=XXXXX@license3.uwo.sharcnet&lt;br /&gt;
cp /opt/sharcnet/local/lsdyna/examples/airbag.deploy.k airbag.deploy.k&lt;br /&gt;
SERIAL JOB:  sqsub -q serial -r 1d -o ofile.%J ls971_d_R4_2_1 i=airbag.deploy.k&lt;br /&gt;
THREADED JOB:  sqsub -q threaded -n 4 -r 1d -o ofile.%J ls971_d_R4_2_1 ncpu=4 para=2 i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note! the &amp;lt;i&amp;gt;ls971_s_R3_1&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;ls971_d_R3_1&amp;lt;/i&amp;gt; binaries do not work, a fix is being looked for.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Homepage&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/products/ls-dyna&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Support (Tutorials, HowTos, Faq, Manuals, Release Notes, News, Links)&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Release Notes&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/release-notes&lt;br /&gt;
&lt;br /&gt;
o LS-PrePost Online Documentation FAQ&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/lspp/content/faq.shtml&lt;br /&gt;
&lt;br /&gt;
o LSTC Download/Install Overview Page&amp;lt;br&amp;gt;&lt;br /&gt;
Provides links to binaries for LS-DYNA SMP/MPP, LS-OPT, LS-PREPOST, LS-TASC.&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/download&lt;br /&gt;
&lt;br /&gt;
==Memory Links==&lt;br /&gt;
&lt;br /&gt;
o Implicit: Memory notes&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.dynasupport.com/howtos/implicit/implicit-memory-notes&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Support Environment variables&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/howtos/general/environment-variables&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA and d3VIEW Blog (a few words on memory settings)&amp;lt;br&amp;gt;&lt;br /&gt;
http://blog2.d3view.com/a-few-words-on-memory-settings-in-ls-dyna/&lt;br /&gt;
&lt;br /&gt;
o Convert Words to GB ie) memory=500MW (3.73GB)&amp;lt;br&amp;gt;&lt;br /&gt;
http://deviceanalytics.com/memcalc.php&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17540</id>
		<title>LSDYNA</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17540"/>
				<updated>2019-02-08T20:14:03Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Graham Cluster */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=LSDYNA&lt;br /&gt;
|package_description=Suite of programs for transient dynamic finite element program&lt;br /&gt;
|package_idnumber=104&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Before a research group can use LSDYNA on sharcnet, a license must be purchased directly from LSTC for the sharcnet license server.  Alternately, if a research group resides at a institution that has sharcnet computing hardware (mac, uwo, guelph, waterloo) it maybe possible to use a pre-existing site license hosted on an accessible institutional license server.   To access and use this software you must open a ticket and request to be added to the sharcnet &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt; group.&lt;br /&gt;
&lt;br /&gt;
=Version Selection=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Create and customize this file for your license server, where XXXXX should be replaced with your port number and Y with your server number:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra1:~] cat .licenses/ls-dyna.lic &lt;br /&gt;
 #LICENSE_TYPE: network&lt;br /&gt;
 #LICENSE_SERVER: XXXXX@licenseY.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The modules are loaded automatically in the testomp.sh and testmpi.sh script shown below in the example section:&lt;br /&gt;
&lt;br /&gt;
For single node smp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna&lt;br /&gt;
        ls-dyna/7.1.2&lt;br /&gt;
        ls-dyna/7.1.3&lt;br /&gt;
        ls-dyna/8.1&lt;br /&gt;
        ls-dyna/9.1&lt;br /&gt;
        ls-dyna/9.2&lt;br /&gt;
        ls-dyna/10.0&lt;br /&gt;
&lt;br /&gt;
To load any version do for example:&lt;br /&gt;
&lt;br /&gt;
 module load ls-dyna/9.1&lt;br /&gt;
&lt;br /&gt;
For multi node mpp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna-mpi&lt;br /&gt;
        ls-dyna-mpi/7.1.2&lt;br /&gt;
        ls-dyna-mpi/7.1.3&lt;br /&gt;
        ls-dyna-mpi/8.1&lt;br /&gt;
        ls-dyna-mpi/9.1&lt;br /&gt;
        ls-dyna-mpi/9.2&lt;br /&gt;
        ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
To load the 7.1.X modules do the following:&lt;br /&gt;
 module load openmpi/1.6.5 ls-dyna-mpi/7.1.3&lt;br /&gt;
&lt;br /&gt;
To load the 8.X or 9.X modules do:&lt;br /&gt;
 module load openmpi/1.8.8 ls-dyna-mpi/9.2&lt;br /&gt;
&lt;br /&gt;
To load the 10.X modules do:&lt;br /&gt;
 module load openmpi/1.10.7 ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Once a license configuration is established, the research group will be given a 5 digit port number.  The value should then be inserted into the appropriate departmental export statement before loading the module file as follows:&lt;br /&gt;
&lt;br /&gt;
o UofT Mechanical Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o McGill Mechanical Engineering Department&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license2.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o UW Mechanical and Mechatronics Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license3.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Laurentian University Bharti School of Engineering&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license4.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Fictitious Example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;12345&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The next step is load the desired sharcnet lsdyna module version.  Check which modules are available by running the &amp;lt;i&amp;gt;module avail&amp;lt;/i&amp;gt; command then load one the modules as shown:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] module avail lsdyna&lt;br /&gt;
------------------------------------ /opt/sharcnet/modules ------------------------------------&lt;br /&gt;
lsdyna/hyb/r711.88920     lsdyna/mpp/r711.88920     lsdyna/smp/r611.79036&lt;br /&gt;
lsdyna/mpp/ls971.r85718   lsdyna/mpp/r712.95028     lsdyna/smp/r711.88920&lt;br /&gt;
lsdyna/mpp/ls980B1.011113 lsdyna/mpp/r800.95359     lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna/mpp/r611.79036     lsdyna/mpp/r901.109912    lsdyna/smp/r800.95359&lt;br /&gt;
lsdyna/mpp/r611.80542     lsdyna/smp/ls980B1.78258  lsdyna/smp/r901.109912&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r9.0.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r901.109912&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r901.109912&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r8.0.0 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r800.95359&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r800.95359&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.2 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r712.95028&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/mpp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r6.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r611.79036&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
or&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.80542&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;i&amp;gt;r611.80542&amp;lt;/i&amp;gt; provides lsdyna_s only.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====ls980 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/ls980B1.78258&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.4.5 lsdyna/mpp/ls980B1.011113&lt;br /&gt;
or&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/smp/ls980B1.78258&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note1) restart capability for ls980smpB1 and ls980mppB1 is not supported&lt;br /&gt;
Note2) the module using legacy openmpi/intel/1.4.5 will run extremely slow&lt;br /&gt;
&lt;br /&gt;
=Job Submission=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
The submission scripts myompjob.sh and mympijob.sh for the airbag problem are shown in the &amp;lt;b&amp;gt;Example Job&amp;lt;/b&amp;gt; section below, for both graham and orca.  Please note that you should specify your own username if submitting to a def account (not roberpj).  Alternatively you could specify your resource allocation account:&lt;br /&gt;
&lt;br /&gt;
Sample threaded job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch myompjob.sh&lt;br /&gt;
&lt;br /&gt;
Sample mpi job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch  mympijob.sh&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
When loading a lsdyna sharcnet legacy module on the new orca or a sharcnet legacy system, the single or double precision solvers are specified with &amp;lt;strong&amp;gt;lsdyna_s&amp;lt;/strong&amp;gt; or &amp;lt;strong&amp;gt;lsdyna_d&amp;lt;/strong&amp;gt; respectively, as shown in the following sqsub commands:&lt;br /&gt;
&lt;br /&gt;
===1cpu SERIAL Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q serial -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===4cpu SMP Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=1G lsdyna_s i=airbag.deploy.k ncpu=4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using an explicit solver, one can specify a conservative initial memory setting on the command line as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=4 memory=754414&amp;lt;/pre&amp;gt;&lt;br /&gt;
where memory is the minimum number of 8 byte words shared by all processors in double precision, or, 4 byte words in single precision.&lt;br /&gt;
&lt;br /&gt;
The initial value can be determined by starting a simulation interactively on the command line, and finding the output resembling: &amp;lt;i&amp;gt; Memory required to begin solution      :      754414&amp;lt;/i&amp;gt;.    The number of words can be specified as memory=260M instead of memory=260000000,  for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  &lt;br /&gt;
&lt;br /&gt;
===8cpu MPI Job===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The initial memory can also be specified for mpi jobs  on the sqsub command line with &amp;quot;memory=&amp;quot; for the first master processor to decompose the problem  and &amp;quot;memory2=&amp;quot; used on all processors including the master to solves the decomposed problem, where the values are specified as 4 bytes per word in single precision and 8 bytes per word in double precision.   The number of words can be specified as memory=260M instead of memory=260000000 OR memory2=260M instead of memory2=260000000 for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  The initial values can be found by running simulation interactively on a orca compute node and checking the output for a line such as: &amp;lt;i&amp;gt;Memory required to begin solution (memory=     464898  memory2=     158794 )&amp;lt;/i&amp;gt; which could then be implemented for a job run in the queue by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898 memory2=158794&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The specification of memory2 on sharcnet or compute canada clusters is not beneficial since the queue reserves the same memory per core for all nodes, such that the decomposition master node process cannot be allocated to have larger system memory.  Therefore its sufficient to specify a single memory parameter by doing:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto &lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the LSTC_MEMORY variable will only allow the memory to grow for explicit simulations. The following slurm examples demonstrate how to prescribe the memory parameters exactly.&lt;br /&gt;
&lt;br /&gt;
=Example Job=&lt;br /&gt;
&lt;br /&gt;
Copy the airbag example to your account:&lt;br /&gt;
&lt;br /&gt;
 rsync -av orca.computecanada.ca:/opt/sharcnet/lsdyna/r901.109912/examples r901.109912_examples&lt;br /&gt;
 cd r901.109912_examples/examples/misc/airbag&lt;br /&gt;
 gunzip airbag.deploy.k.gz&lt;br /&gt;
&lt;br /&gt;
==Graham (default modules)==&lt;br /&gt;
&lt;br /&gt;
Please note that graham does not have the sharcnet legacy modules installed on it.&lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mysmpjob1.sh&amp;lt;/b&amp;gt; for 4 core single precision smp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=4                   # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testsmp1                 # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load ls-dyna/9.1&lt;br /&gt;
ls-dyna_s i=airbag.deploy.k ncpu=4 memory=1000M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 4 (bytes/word) = 1000M words&lt;br /&gt;
&lt;br /&gt;
===Mpi Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mympijob2.sh&amp;lt;/b&amp;gt; for 4 core double precision mpp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --ntasks=4                     # number of ranks&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M            # memory per rank&lt;br /&gt;
#SBATCH --time=00:30:00                # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testmpi2            # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load openmpi/1.8.8 ls-dyna-mpi/9.1&lt;br /&gt;
srun ls-dyna_d i=airbag.deploy.k memory=500M memory2=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
==New Orca (legacy modules)==&lt;br /&gt;
&lt;br /&gt;
Please note, while new orca has most compute canada cvmfs modules available by default it does not have the graham the &amp;lt;i&amp;gt;ls-dyna&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;ls-dyna-mpi&amp;lt;/i&amp;gt; modules installed.  Therefore currently the only way to run lsdyna is by using the legacy sharcnet &amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt; modules as shown in the following two smp and mpi examples. &lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Submission script &amp;lt;b&amp;gt;mysmpjob3.sh&amp;lt;/b&amp;gt; to run 16 core single precision single node smp job with sharcnet legacy modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=16                  # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=mysmpjob3-1node-16core    # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server for legacy modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=31047@license3.sharcnet.ca&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does NOT work with legacy modules&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna_s i=airbag.deploy.k ncpu=$SLURM_CPUS_PER_TASK memory=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
===MPI Job ===&lt;br /&gt;
&lt;br /&gt;
To run the sharcnet legacy &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; efficiently over several compute nodes with many cores requires using orca's xeon nodes as shown in &amp;lt;b&amp;gt; mympijob4.sh&amp;lt;/b&amp;gt; below.   To run lsdyna on orca's opteron nodes with &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; requires using only &amp;lt;b&amp;gt;one&amp;lt;/b&amp;gt; opteron node as shown in &amp;lt;b&amp;gt; mympijob5.sh&amp;lt;/b&amp;gt; below.  The other legacy sharcnet lsdyna mpp modules, as shown in the Availability Table  lsdyna/mpp/ls971.r85718, lsdyna/mpp/ls980B1.011113, lsdyna/mpp/r611.79036, lsdyna/mpp/r611.80542, lsdyna/mpp/r711.88920, lsdyna/mpp/r800.95359 and lsdyna/mpp/r901.109912 have yet to be tested. If you want to use one but cannot get it working please open a ticket.  Note that lines containing a space between &amp;quot;# and SBATCH&amp;quot; are comments.  To activate such line(s) required removal of the space ie) &amp;quot;#SBATCH&amp;quot;.  Its recommended to use &amp;lt;i&amp;gt;module ls-dyna-mpi&amp;lt;/i&amp;gt; on graham instead since the machine is far newer, faster and reliable. &lt;br /&gt;
&lt;br /&gt;
====Multi Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob4.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=xeon&lt;br /&gt;
#SBATCH --ntasks=48&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob4-Xnodes-48cores-xeon  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out             # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE --mca plm_slurm_args '--cpu_bind=none' lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob5.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=opteron&lt;br /&gt;
#SBATCH --nodes=1&lt;br /&gt;
# SBATCH --ntasks=24&lt;br /&gt;
# SBATCH --mem=0&lt;br /&gt;
#SBATCH --ntasks=16&lt;br /&gt;
#SBATCH --mem-per-cpu=1000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob5-1node-16cores-opteron  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 1000M / 4 (bytes/word) = 250M words&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
STEP1) The following shows sqsub submission of the [http://www.dynaexamples.com/examples-manual/misc/airbag airbag] example to the mpi queue.  Its recommended to first edit  &amp;lt;i&amp;gt;airbag.deploy.k&amp;lt;/i&amp;gt;  and change &amp;lt;i&amp;gt;endtim&amp;lt;/i&amp;gt; to 3.000E-00 so the job runs long enough to perform the restart in steps 2 and 3 below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp -a /opt/sharcnet/lsdyna/r611.79036/examples /scratch/$USER/test-lsdyna&lt;br /&gt;
cd /scratch/$USER/test-lsdyna/misc/airbag&lt;br /&gt;
gunzip airbag.deploy.k.gz&lt;br /&gt;
cp airbag.deploy.k airbag.deploy.restart.k&lt;br /&gt;
nano deploy.restart.k (reduce plot file creation frequency)&lt;br /&gt;
   *DATABASE_BINARY_D3PLOT&lt;br /&gt;
   $       dt      lcdtdeploy.restart.k&lt;br /&gt;
   1.000E-01   &amp;lt;--- change from 5.000E-04 &lt;br /&gt;
export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
sqsub -r 10m -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP2) With the job still running, use the echo command as follows to create a file called &amp;quot;D3KIL&amp;quot; that will trigger generation of restart files at which point the file D3KIL itself will be erased.  Do this once a day if data loss is critical to you OR once or twice just before the  &amp;lt;i&amp;gt;sqsub -r&amp;lt;/i&amp;gt; time limit is reached.  Further information can be found here http://www.dynasupport.com/tutorial/ls-dyna-users-guide/sense-switch-control:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
echo &amp;quot;sw3&amp;quot; &amp;gt; D3KIL&lt;br /&gt;
sqkill job#&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP3) Before the job can be restarted, the following two lines must be added to the &amp;lt;i&amp;gt;airbag.deploy.restart.k&amp;lt;/i&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*CHANGE_CURVE_DEFINITION&lt;br /&gt;
         1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP4) Now resubmit the job as follows using &amp;quot;r=&amp;quot; to specify the restart file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sqsub -r 1h -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.restart.k r=d3dump01&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=General Notes=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
There are no special notes at the present time in regards to using lsdyna on graham.&lt;br /&gt;
&lt;br /&gt;
==Command Line Use==&lt;br /&gt;
&lt;br /&gt;
Parallel lsdyna jobs can be run interactively on the command line (outside the queue) for short run testing purposes as follows:&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node MPP (mpi) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/mpp/r711.88920&lt;br /&gt;
 mpirun -np 8 lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node SMP (threaded) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load lsdyna/smp/r711.88920&lt;br /&gt;
 lsdyna_d i=airbag.deploy.k ncpu=8&lt;br /&gt;
&lt;br /&gt;
==Memory Issues==&lt;br /&gt;
&lt;br /&gt;
A minumum of mpp=2G is recommended although the memory requirement of a job may suggest much less is required. For instance setting mpp=1G for the airbag test job above will result in the following error when running a job in the queue:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
An attempt to set processor affinity has failed - please check to&lt;br /&gt;
ensure that your system supports such functionality. If so, then&lt;br /&gt;
this is probably something that should be reported to the OMPI developers.&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another error message which can occur after a job runs for a while if mpp is chosen to small is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Process 45 &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Signal 11 : Segmentation Violation &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To get an idea of the amount of memory a job used run grep on the output file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep Memory&lt;br /&gt;
     |    Distributed Memory Parallel                  |&lt;br /&gt;
 Memory size from command line:      500000,       500000&lt;br /&gt;
 Memory for the head node&lt;br /&gt;
 Memory installed (MB)        :        32237&lt;br /&gt;
 Memory free (MB)             :        11259&lt;br /&gt;
 Memory required (MB)         :            0&lt;br /&gt;
 Memory required to process keyword     :       458120&lt;br /&gt;
 Memory required for decomposition      :       458120&lt;br /&gt;
 Memory required to begin solution (memory=      458120 memory2=      230721)&lt;br /&gt;
 Max. Memory reqd for implicit sol: max used               0&lt;br /&gt;
 Max. Memory reqd for implicit sol: incore                 0&lt;br /&gt;
 Max. Memory reqd for implicit sol: oocore                 0&lt;br /&gt;
 Memory required to complete solution (memory=      458120 memory2=      230721)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Version and Revision==&lt;br /&gt;
&lt;br /&gt;
Again run grep on the output file to extract the major and minor revision:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep 'Revision\|Version'&lt;br /&gt;
     |  Version : mpp s R6.1.1    Date: 01/02/2013     |&lt;br /&gt;
     |  Revision: 78769           Time: 07:43:30       |&lt;br /&gt;
     |  SVN Version: 80542                             |&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Check License Status==&lt;br /&gt;
&lt;br /&gt;
===Running and Queued Programs===&lt;br /&gt;
&lt;br /&gt;
To get a summary of running and/or queued jobs use the &amp;lt;i&amp;gt;lstc_qrun&amp;lt;/i&amp;gt; command as follows, where queued means the job has started running according to the sqsub command but its actually sitting waiting for license resources to come available.  Once these are acquired the job will start running on the cluster and appear as a Running Program according to  lstc_qrun ie)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
                     Running Programs&lt;br /&gt;
    User             Host          Program              Started       # procs&lt;br /&gt;
-----------------------------------------------------------------------------&lt;br /&gt;
dgierczy    20277@saw61.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dgierczy     8570@saw32.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dscronin    25486@hnd6             MPPDYNA_971      Tue Apr  9 20:19     6&lt;br /&gt;
dscronin    14897@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    14971@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    15046@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    31237@hnd16            MPPDYNA_971      Tue Apr  9 21:53     6&lt;br /&gt;
dscronin    31313@hnd16            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
dscronin     6396@hnd15            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
csharson    28890@saw175.saw.sharc MPPDYNA_971      Wed Apr 10 16:48     6&lt;br /&gt;
No programs queued&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To show license file expiration details, append &amp;quot;-R&amp;quot; to the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun -R&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
**** LICENSE INFORMATION ****&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-DYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dgierczy   20277@saw61.saw.sharcnet   1&lt;br /&gt;
 dgierczy    8570@saw32.saw.sharcnet   1&lt;br /&gt;
MPPDYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dscronin   25486@hnd6               6&lt;br /&gt;
 dscronin   14897@hnd18              6&lt;br /&gt;
 dscronin   14971@hnd18              6&lt;br /&gt;
 dscronin   15046@hnd18              6&lt;br /&gt;
 dscronin   31237@hnd16              6&lt;br /&gt;
 dscronin   31313@hnd16              6&lt;br /&gt;
 dscronin    6396@hnd15              6&lt;br /&gt;
 csharson   28890@saw175.saw.sharcnet   6&lt;br /&gt;
                   LICENSE GROUP    58    966   1024 |     0&lt;br /&gt;
&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-OPT           12/31/2013          0   1024   1024 |     0&lt;br /&gt;
                   LICENSE GROUP     0   1024   1024 |     0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Killling A Program===&lt;br /&gt;
&lt;br /&gt;
The queue normally kills lsdyna jobs cleanly.  However its possible that licences for a job (which is no longer running in the queue and therefore no longer has any processes running on the cluster) will continue to be tied up according to the lstc_qrun command.   To kill such a program determine the pid@hostname from the lstc_qrun command then run the following kill command.  The following example demonstrates the procedure for username roberpj:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
roberpj    45312@orc329.orca.shar MPPDYNA          Wed Dec  5 15:50    12&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~] lstc_qkill 45312@orc329.orca.sharcnet.ca&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
Program queued for termination&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
[roberpj@orc-login2:~] &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Legacy Instructions==&lt;br /&gt;
&lt;br /&gt;
The following binaries remain available on saw and orca for backward compatibility testing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] cd /opt/sharcnet/local/lsdyna&lt;br /&gt;
[roberpj@orc129:/opt/sharcnet/local/lsdyna] ls ls971*&lt;br /&gt;
ls971_d_R3_1    ls971_d_R4_2_1  ls971_s_R3_1    ls971_s_R4_2_1  ls971_s_R5_1_1&lt;br /&gt;
ls971_d_R4_2_0  ls971_d_R5_0    ls971_s_R4_2_0  ls971_s_R5_0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are currently no sharcnet modules for these versions, hence jobs should be submitted as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load lsdyna&lt;br /&gt;
export PATH=/opt/sharcnet/local/lsdyna:$PATH&lt;br /&gt;
export LSTC_LICENSE_SERVER=XXXXX@license3.uwo.sharcnet&lt;br /&gt;
cp /opt/sharcnet/local/lsdyna/examples/airbag.deploy.k airbag.deploy.k&lt;br /&gt;
SERIAL JOB:  sqsub -q serial -r 1d -o ofile.%J ls971_d_R4_2_1 i=airbag.deploy.k&lt;br /&gt;
THREADED JOB:  sqsub -q threaded -n 4 -r 1d -o ofile.%J ls971_d_R4_2_1 ncpu=4 para=2 i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note! the &amp;lt;i&amp;gt;ls971_s_R3_1&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;ls971_d_R3_1&amp;lt;/i&amp;gt; binaries do not work, a fix is being looked for.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Homepage&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/products/ls-dyna&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Support (Tutorials, HowTos, Faq, Manuals, Release Notes, News, Links)&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Release Notes&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/release-notes&lt;br /&gt;
&lt;br /&gt;
o LS-PrePost Online Documentation FAQ&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/lspp/content/faq.shtml&lt;br /&gt;
&lt;br /&gt;
o LSTC Download/Install Overview Page&amp;lt;br&amp;gt;&lt;br /&gt;
Provides links to binaries for LS-DYNA SMP/MPP, LS-OPT, LS-PREPOST, LS-TASC.&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/download&lt;br /&gt;
&lt;br /&gt;
==Memory Links==&lt;br /&gt;
&lt;br /&gt;
o Implicit: Memory notes&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.dynasupport.com/howtos/implicit/implicit-memory-notes&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Support Environment variables&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/howtos/general/environment-variables&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA and d3VIEW Blog (a few words on memory settings)&amp;lt;br&amp;gt;&lt;br /&gt;
http://blog2.d3view.com/a-few-words-on-memory-settings-in-ls-dyna/&lt;br /&gt;
&lt;br /&gt;
o Convert Words to GB ie) memory=500MW (3.73GB)&amp;lt;br&amp;gt;&lt;br /&gt;
http://deviceanalytics.com/memcalc.php&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17539</id>
		<title>LSDYNA</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17539"/>
				<updated>2019-02-08T20:13:27Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Graham Cluster */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=LSDYNA&lt;br /&gt;
|package_description=Suite of programs for transient dynamic finite element program&lt;br /&gt;
|package_idnumber=104&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Before a research group can use LSDYNA on sharcnet, a license must be purchased directly from LSTC for the sharcnet license server.  Alternately, if a research group resides at a institution that has sharcnet computing hardware (mac, uwo, guelph, waterloo) it maybe possible to use a pre-existing site license hosted on an accessible institutional license server.   To access and use this software you must open a ticket and request to be added to the sharcnet &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt; group.&lt;br /&gt;
&lt;br /&gt;
=Version Selection=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Create and customize this file for your license server, where XXXXX should be replaced with your port number and Y with your server number:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra1:~] cat .licenses/ls-dyna.lic &lt;br /&gt;
 #LICENSE_TYPE: network&lt;br /&gt;
 #LICENSE_SERVER: XXXXX@licenseY.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The modules are loaded automatically in the testomp.sh and testmpi.sh script shown below in the example section:&lt;br /&gt;
&lt;br /&gt;
For single node smp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna&lt;br /&gt;
        ls-dyna/7.1.2&lt;br /&gt;
        ls-dyna/7.1.3&lt;br /&gt;
        ls-dyna/8.1&lt;br /&gt;
        ls-dyna/9.1&lt;br /&gt;
        ls-dyna/9.2&lt;br /&gt;
        ls-dyna/10.0&lt;br /&gt;
&lt;br /&gt;
To load any version do for example:&lt;br /&gt;
&lt;br /&gt;
 module load ls-dyna/9.1&lt;br /&gt;
&lt;br /&gt;
For multi node mpp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna-mpi&lt;br /&gt;
        ls-dyna-mpi/7.1.2&lt;br /&gt;
        ls-dyna-mpi/7.1.3&lt;br /&gt;
        ls-dyna-mpi/8.1&lt;br /&gt;
        ls-dyna-mpi/9.1&lt;br /&gt;
        ls-dyna-mpi/9.2&lt;br /&gt;
        ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
To load the 7.1.X modules do the following:&lt;br /&gt;
 module load openmpi/1.6.5 ls-dyna-mpi/7.1.3&lt;br /&gt;
&lt;br /&gt;
To load the 8.X or 9.X modules do:&lt;br /&gt;
 module load openmpi/1.8.8 ls-dyna-mpi/9.2&lt;br /&gt;
&lt;br /&gt;
To load the 10.X modules do:&lt;br /&gt;
 module load openmpi/1.10.7 ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Once a license configuration is established, the research group will be given a 5 digit port number.  The value should then be inserted into the appropriate departmental export statement before loading the module file as follows:&lt;br /&gt;
&lt;br /&gt;
o UofT Mechanical Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o McGill Mechanical Engineering Department&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license2.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o UW Mechanical and Mechatronics Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license3.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Laurentian University Bharti School of Engineering&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license4.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Fictitious Example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;12345&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The next step is load the desired sharcnet lsdyna module version.  Check which modules are available by running the &amp;lt;i&amp;gt;module avail&amp;lt;/i&amp;gt; command then load one the modules as shown:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] module avail lsdyna&lt;br /&gt;
------------------------------------ /opt/sharcnet/modules ------------------------------------&lt;br /&gt;
lsdyna/hyb/r711.88920     lsdyna/mpp/r711.88920     lsdyna/smp/r611.79036&lt;br /&gt;
lsdyna/mpp/ls971.r85718   lsdyna/mpp/r712.95028     lsdyna/smp/r711.88920&lt;br /&gt;
lsdyna/mpp/ls980B1.011113 lsdyna/mpp/r800.95359     lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna/mpp/r611.79036     lsdyna/mpp/r901.109912    lsdyna/smp/r800.95359&lt;br /&gt;
lsdyna/mpp/r611.80542     lsdyna/smp/ls980B1.78258  lsdyna/smp/r901.109912&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r9.0.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r901.109912&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r901.109912&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r8.0.0 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r800.95359&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r800.95359&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.2 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r712.95028&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/mpp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r6.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r611.79036&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
or&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.80542&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;i&amp;gt;r611.80542&amp;lt;/i&amp;gt; provides lsdyna_s only.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====ls980 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/ls980B1.78258&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.4.5 lsdyna/mpp/ls980B1.011113&lt;br /&gt;
or&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/smp/ls980B1.78258&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note1) restart capability for ls980smpB1 and ls980mppB1 is not supported&lt;br /&gt;
Note2) the module using legacy openmpi/intel/1.4.5 will run extremely slow&lt;br /&gt;
&lt;br /&gt;
=Job Submission=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
The submission scripts myompjob.sh and mympijob.sh for the airbag problem are shown in the &amp;lt;b&amp;gt;Example Job&amp;lt;/b&amp;gt; section below, for both graham and orca.  Please note that you should specify your own username if submitting to a def account (not roberpj).  Alternatively you could specify your resource allocation account:&lt;br /&gt;
&lt;br /&gt;
Sample threaded job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch myompjob.sh&lt;br /&gt;
&lt;br /&gt;
Sample mpi job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch  mympijob.sh&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
When loading a lsdyna sharcnet legacy module on the new orca or a sharcnet legacy system, the single or double precision solvers are specified with &amp;lt;strong&amp;gt;lsdyna_s&amp;lt;/strong&amp;gt; or &amp;lt;strong&amp;gt;lsdyna_d&amp;lt;/strong&amp;gt; respectively, as shown in the following sqsub commands:&lt;br /&gt;
&lt;br /&gt;
===1cpu SERIAL Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q serial -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===4cpu SMP Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=1G lsdyna_s i=airbag.deploy.k ncpu=4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using an explicit solver, one can specify a conservative initial memory setting on the command line as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=4 memory=754414&amp;lt;/pre&amp;gt;&lt;br /&gt;
where memory is the minimum number of 8 byte words shared by all processors in double precision, or, 4 byte words in single precision.&lt;br /&gt;
&lt;br /&gt;
The initial value can be determined by starting a simulation interactively on the command line, and finding the output resembling: &amp;lt;i&amp;gt; Memory required to begin solution      :      754414&amp;lt;/i&amp;gt;.    The number of words can be specified as memory=260M instead of memory=260000000,  for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  &lt;br /&gt;
&lt;br /&gt;
===8cpu MPI Job===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The initial memory can also be specified for mpi jobs  on the sqsub command line with &amp;quot;memory=&amp;quot; for the first master processor to decompose the problem  and &amp;quot;memory2=&amp;quot; used on all processors including the master to solves the decomposed problem, where the values are specified as 4 bytes per word in single precision and 8 bytes per word in double precision.   The number of words can be specified as memory=260M instead of memory=260000000 OR memory2=260M instead of memory2=260000000 for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  The initial values can be found by running simulation interactively on a orca compute node and checking the output for a line such as: &amp;lt;i&amp;gt;Memory required to begin solution (memory=     464898  memory2=     158794 )&amp;lt;/i&amp;gt; which could then be implemented for a job run in the queue by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898 memory2=158794&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The specification of memory2 on sharcnet or compute canada clusters is not beneficial since the queue reserves the same memory per core for all nodes, such that the decomposition master node process cannot be allocated to have larger system memory.  Therefore its sufficient to specify a single memory parameter by doing:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto &lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the LSTC_MEMORY variable will only allow the memory to grow for explicit simulations. The following slurm examples demonstrate how to prescribe the memory parameters exactly.&lt;br /&gt;
&lt;br /&gt;
=Example Job=&lt;br /&gt;
&lt;br /&gt;
Copy the airbag example to your account:&lt;br /&gt;
&lt;br /&gt;
 rsync -av orca.computecanada.ca:/opt/sharcnet/lsdyna/r901.109912/examples r901.109912_examples&lt;br /&gt;
 cd r901.109912_examples/examples/misc/airbag&lt;br /&gt;
 gunzip airbag.deploy.k.gz&lt;br /&gt;
&lt;br /&gt;
==Graham (default modules)==&lt;br /&gt;
&lt;br /&gt;
Please note that graham does not have the sharcnet legacy modules installed on it.&lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mysmpjob1.sh&amp;lt;/b&amp;gt; for 4 core single precision smp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=4                   # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testsmp1                 # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load ls-dyna/9.1&lt;br /&gt;
ls-dyna_s i=airbag.deploy.k ncpu=4 memory=1000M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 4 (bytes/word) = 1000M words&lt;br /&gt;
&lt;br /&gt;
===Mpi Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mympijob2.sh&amp;lt;/b&amp;gt; for 4 core double precision mpp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --ntasks=4                     # number of ranks&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M            # memory per rank&lt;br /&gt;
#SBATCH --time=00:30:00                # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testmpi2            # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load openmpi/1.8.8 ls-dyna-mpi/9.1&lt;br /&gt;
srun ls-dyna_d i=airbag.deploy.k memory=500M memory2=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
==New Orca (legacy modules)==&lt;br /&gt;
&lt;br /&gt;
Please note, while new orca has most compute canada cvmfs modules available by default it does not have the graham the &amp;lt;i&amp;gt;ls-dyna&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;ls-dyna-mpi&amp;lt;/i&amp;gt; modules installed.  Therefore currently the only way to run lsdyna is by using the legacy sharcnet &amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt; modules as shown in the following two smp and mpi examples. &lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Submission script &amp;lt;b&amp;gt;mysmpjob3.sh&amp;lt;/b&amp;gt; to run 16 core single precision single node smp job with sharcnet legacy modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=16                  # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=mysmpjob3-1node-16core    # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server for legacy modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=31047@license3.sharcnet.ca&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does NOT work with legacy modules&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna_s i=airbag.deploy.k ncpu=$SLURM_CPUS_PER_TASK memory=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
===MPI Job ===&lt;br /&gt;
&lt;br /&gt;
To run the sharcnet legacy &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; efficiently over several compute nodes with many cores requires using orca's xeon nodes as shown in &amp;lt;b&amp;gt; mympijob4.sh&amp;lt;/b&amp;gt; below.   To run lsdyna on orca's opteron nodes with &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; requires using only &amp;lt;b&amp;gt;one&amp;lt;/b&amp;gt; opteron node as shown in &amp;lt;b&amp;gt; mympijob5.sh&amp;lt;/b&amp;gt; below.  The other legacy sharcnet lsdyna mpp modules, as shown in the Availability Table  lsdyna/mpp/ls971.r85718, lsdyna/mpp/ls980B1.011113, lsdyna/mpp/r611.79036, lsdyna/mpp/r611.80542, lsdyna/mpp/r711.88920, lsdyna/mpp/r800.95359 and lsdyna/mpp/r901.109912 have yet to be tested. If you want to use one but cannot get it working please open a ticket.  Note that lines containing a space between &amp;quot;# and SBATCH&amp;quot; are comments.  To activate such line(s) required removal of the space ie) &amp;quot;#SBATCH&amp;quot;.  Its recommended to use &amp;lt;i&amp;gt;module ls-dyna-mpi&amp;lt;/i&amp;gt; on graham instead since the machine is far newer, faster and reliable. &lt;br /&gt;
&lt;br /&gt;
====Multi Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob4.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=xeon&lt;br /&gt;
#SBATCH --ntasks=48&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob4-Xnodes-48cores-xeon  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out             # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE --mca plm_slurm_args '--cpu_bind=none' lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob5.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=opteron&lt;br /&gt;
#SBATCH --nodes=1&lt;br /&gt;
# SBATCH --ntasks=24&lt;br /&gt;
# SBATCH --mem=0&lt;br /&gt;
#SBATCH --ntasks=16&lt;br /&gt;
#SBATCH --mem-per-cpu=1000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob5-1node-16cores-opteron  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 1000M / 4 (bytes/word) = 250M words&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
STEP1) The following shows sqsub submission of the [http://www.dynaexamples.com/examples-manual/misc/airbag airbag] example to the mpi queue.  Its recommended to first edit  &amp;lt;i&amp;gt;airbag.deploy.k&amp;lt;/i&amp;gt;  and change &amp;lt;i&amp;gt;endtim&amp;lt;/i&amp;gt; to 3.000E-00 so the job runs long enough to perform the restart in steps 2 and 3 below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp -a /opt/sharcnet/lsdyna/r611.79036/examples /scratch/$USER/test-lsdyna&lt;br /&gt;
cd /scratch/$USER/test-lsdyna/misc/airbag&lt;br /&gt;
gunzip airbag.deploy.k.gz&lt;br /&gt;
cp airbag.deploy.k airbag.deploy.restart.k&lt;br /&gt;
nano deploy.restart.k (reduce plot file creation frequency)&lt;br /&gt;
   *DATABASE_BINARY_D3PLOT&lt;br /&gt;
   $       dt      lcdtdeploy.restart.k&lt;br /&gt;
   1.000E-01   &amp;lt;--- change from 5.000E-04 &lt;br /&gt;
export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
sqsub -r 10m -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP2) With the job still running, use the echo command as follows to create a file called &amp;quot;D3KIL&amp;quot; that will trigger generation of restart files at which point the file D3KIL itself will be erased.  Do this once a day if data loss is critical to you OR once or twice just before the  &amp;lt;i&amp;gt;sqsub -r&amp;lt;/i&amp;gt; time limit is reached.  Further information can be found here http://www.dynasupport.com/tutorial/ls-dyna-users-guide/sense-switch-control:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
echo &amp;quot;sw3&amp;quot; &amp;gt; D3KIL&lt;br /&gt;
sqkill job#&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP3) Before the job can be restarted, the following two lines must be added to the &amp;lt;i&amp;gt;airbag.deploy.restart.k&amp;lt;/i&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*CHANGE_CURVE_DEFINITION&lt;br /&gt;
         1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP4) Now resubmit the job as follows using &amp;quot;r=&amp;quot; to specify the restart file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sqsub -r 1h -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.restart.k r=d3dump01&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=General Notes=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
There are no special notes at present regarding the usage of lsdyna on graham,&lt;br /&gt;
&lt;br /&gt;
==Command Line Use==&lt;br /&gt;
&lt;br /&gt;
Parallel lsdyna jobs can be run interactively on the command line (outside the queue) for short run testing purposes as follows:&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node MPP (mpi) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/mpp/r711.88920&lt;br /&gt;
 mpirun -np 8 lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node SMP (threaded) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load lsdyna/smp/r711.88920&lt;br /&gt;
 lsdyna_d i=airbag.deploy.k ncpu=8&lt;br /&gt;
&lt;br /&gt;
==Memory Issues==&lt;br /&gt;
&lt;br /&gt;
A minumum of mpp=2G is recommended although the memory requirement of a job may suggest much less is required. For instance setting mpp=1G for the airbag test job above will result in the following error when running a job in the queue:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
An attempt to set processor affinity has failed - please check to&lt;br /&gt;
ensure that your system supports such functionality. If so, then&lt;br /&gt;
this is probably something that should be reported to the OMPI developers.&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another error message which can occur after a job runs for a while if mpp is chosen to small is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Process 45 &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Signal 11 : Segmentation Violation &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To get an idea of the amount of memory a job used run grep on the output file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep Memory&lt;br /&gt;
     |    Distributed Memory Parallel                  |&lt;br /&gt;
 Memory size from command line:      500000,       500000&lt;br /&gt;
 Memory for the head node&lt;br /&gt;
 Memory installed (MB)        :        32237&lt;br /&gt;
 Memory free (MB)             :        11259&lt;br /&gt;
 Memory required (MB)         :            0&lt;br /&gt;
 Memory required to process keyword     :       458120&lt;br /&gt;
 Memory required for decomposition      :       458120&lt;br /&gt;
 Memory required to begin solution (memory=      458120 memory2=      230721)&lt;br /&gt;
 Max. Memory reqd for implicit sol: max used               0&lt;br /&gt;
 Max. Memory reqd for implicit sol: incore                 0&lt;br /&gt;
 Max. Memory reqd for implicit sol: oocore                 0&lt;br /&gt;
 Memory required to complete solution (memory=      458120 memory2=      230721)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Version and Revision==&lt;br /&gt;
&lt;br /&gt;
Again run grep on the output file to extract the major and minor revision:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep 'Revision\|Version'&lt;br /&gt;
     |  Version : mpp s R6.1.1    Date: 01/02/2013     |&lt;br /&gt;
     |  Revision: 78769           Time: 07:43:30       |&lt;br /&gt;
     |  SVN Version: 80542                             |&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Check License Status==&lt;br /&gt;
&lt;br /&gt;
===Running and Queued Programs===&lt;br /&gt;
&lt;br /&gt;
To get a summary of running and/or queued jobs use the &amp;lt;i&amp;gt;lstc_qrun&amp;lt;/i&amp;gt; command as follows, where queued means the job has started running according to the sqsub command but its actually sitting waiting for license resources to come available.  Once these are acquired the job will start running on the cluster and appear as a Running Program according to  lstc_qrun ie)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
                     Running Programs&lt;br /&gt;
    User             Host          Program              Started       # procs&lt;br /&gt;
-----------------------------------------------------------------------------&lt;br /&gt;
dgierczy    20277@saw61.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dgierczy     8570@saw32.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dscronin    25486@hnd6             MPPDYNA_971      Tue Apr  9 20:19     6&lt;br /&gt;
dscronin    14897@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    14971@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    15046@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    31237@hnd16            MPPDYNA_971      Tue Apr  9 21:53     6&lt;br /&gt;
dscronin    31313@hnd16            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
dscronin     6396@hnd15            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
csharson    28890@saw175.saw.sharc MPPDYNA_971      Wed Apr 10 16:48     6&lt;br /&gt;
No programs queued&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To show license file expiration details, append &amp;quot;-R&amp;quot; to the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun -R&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
**** LICENSE INFORMATION ****&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-DYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dgierczy   20277@saw61.saw.sharcnet   1&lt;br /&gt;
 dgierczy    8570@saw32.saw.sharcnet   1&lt;br /&gt;
MPPDYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dscronin   25486@hnd6               6&lt;br /&gt;
 dscronin   14897@hnd18              6&lt;br /&gt;
 dscronin   14971@hnd18              6&lt;br /&gt;
 dscronin   15046@hnd18              6&lt;br /&gt;
 dscronin   31237@hnd16              6&lt;br /&gt;
 dscronin   31313@hnd16              6&lt;br /&gt;
 dscronin    6396@hnd15              6&lt;br /&gt;
 csharson   28890@saw175.saw.sharcnet   6&lt;br /&gt;
                   LICENSE GROUP    58    966   1024 |     0&lt;br /&gt;
&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-OPT           12/31/2013          0   1024   1024 |     0&lt;br /&gt;
                   LICENSE GROUP     0   1024   1024 |     0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Killling A Program===&lt;br /&gt;
&lt;br /&gt;
The queue normally kills lsdyna jobs cleanly.  However its possible that licences for a job (which is no longer running in the queue and therefore no longer has any processes running on the cluster) will continue to be tied up according to the lstc_qrun command.   To kill such a program determine the pid@hostname from the lstc_qrun command then run the following kill command.  The following example demonstrates the procedure for username roberpj:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
roberpj    45312@orc329.orca.shar MPPDYNA          Wed Dec  5 15:50    12&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~] lstc_qkill 45312@orc329.orca.sharcnet.ca&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
Program queued for termination&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
[roberpj@orc-login2:~] &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Legacy Instructions==&lt;br /&gt;
&lt;br /&gt;
The following binaries remain available on saw and orca for backward compatibility testing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] cd /opt/sharcnet/local/lsdyna&lt;br /&gt;
[roberpj@orc129:/opt/sharcnet/local/lsdyna] ls ls971*&lt;br /&gt;
ls971_d_R3_1    ls971_d_R4_2_1  ls971_s_R3_1    ls971_s_R4_2_1  ls971_s_R5_1_1&lt;br /&gt;
ls971_d_R4_2_0  ls971_d_R5_0    ls971_s_R4_2_0  ls971_s_R5_0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are currently no sharcnet modules for these versions, hence jobs should be submitted as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load lsdyna&lt;br /&gt;
export PATH=/opt/sharcnet/local/lsdyna:$PATH&lt;br /&gt;
export LSTC_LICENSE_SERVER=XXXXX@license3.uwo.sharcnet&lt;br /&gt;
cp /opt/sharcnet/local/lsdyna/examples/airbag.deploy.k airbag.deploy.k&lt;br /&gt;
SERIAL JOB:  sqsub -q serial -r 1d -o ofile.%J ls971_d_R4_2_1 i=airbag.deploy.k&lt;br /&gt;
THREADED JOB:  sqsub -q threaded -n 4 -r 1d -o ofile.%J ls971_d_R4_2_1 ncpu=4 para=2 i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note! the &amp;lt;i&amp;gt;ls971_s_R3_1&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;ls971_d_R3_1&amp;lt;/i&amp;gt; binaries do not work, a fix is being looked for.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Homepage&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/products/ls-dyna&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Support (Tutorials, HowTos, Faq, Manuals, Release Notes, News, Links)&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Release Notes&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/release-notes&lt;br /&gt;
&lt;br /&gt;
o LS-PrePost Online Documentation FAQ&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/lspp/content/faq.shtml&lt;br /&gt;
&lt;br /&gt;
o LSTC Download/Install Overview Page&amp;lt;br&amp;gt;&lt;br /&gt;
Provides links to binaries for LS-DYNA SMP/MPP, LS-OPT, LS-PREPOST, LS-TASC.&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/download&lt;br /&gt;
&lt;br /&gt;
==Memory Links==&lt;br /&gt;
&lt;br /&gt;
o Implicit: Memory notes&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.dynasupport.com/howtos/implicit/implicit-memory-notes&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Support Environment variables&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/howtos/general/environment-variables&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA and d3VIEW Blog (a few words on memory settings)&amp;lt;br&amp;gt;&lt;br /&gt;
http://blog2.d3view.com/a-few-words-on-memory-settings-in-ls-dyna/&lt;br /&gt;
&lt;br /&gt;
o Convert Words to GB ie) memory=500MW (3.73GB)&amp;lt;br&amp;gt;&lt;br /&gt;
http://deviceanalytics.com/memcalc.php&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17221</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17221"/>
				<updated>2018-12-13T01:12:21Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) For future logins, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above steps can also be followed to start over if a previously installed nix package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17220</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17220"/>
				<updated>2018-12-13T01:11:59Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) For future logins, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above steps can also be followed to start over if a previously installed nix package is not showing up in your path as expected, after loading the nix module.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17219</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17219"/>
				<updated>2018-12-13T01:11:07Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) For future logins, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above example can also be followed to start over if a previously installed nix package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17218</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17218"/>
				<updated>2018-12-13T01:10:39Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) For future logins, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TIP: The above example can also be followed to reset your environment if a previously installed nix package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17217</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17217"/>
				<updated>2018-12-13T01:10:01Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
This example can also be followed to reset your environment if a previously installed nix package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) For future logins, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17216</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17216"/>
				<updated>2018-12-13T01:08:06Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
This example can also be followed to reset your environment if a previously installed nix package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17215</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17215"/>
				<updated>2018-12-13T01:07:53Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
This example can also be follows if a package to reset your environment if a previously installed nix package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17214</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17214"/>
				<updated>2018-12-13T01:06:55Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
This example can also be follows if a package is not showing up in your path as expected ...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17213</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17213"/>
				<updated>2018-12-13T01:06:28Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
New users can jump directly to step 2.  This example is also worth following if a package is not showing up in your path as expected.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17212</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17212"/>
				<updated>2018-12-13T01:05:12Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* With sqsub = */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ==&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17211</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17211"/>
				<updated>2018-12-13T01:05:04Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Submitting Jobs */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
== With sqsub ===&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17210</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17210"/>
				<updated>2018-12-13T01:04:31Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17209</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17209"/>
				<updated>2018-12-13T01:04:03Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17208</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17208"/>
				<updated>2018-12-13T00:21:49Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17207</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17207"/>
				<updated>2018-12-13T00:21:11Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Example(s) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17206</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17206"/>
				<updated>2018-12-13T00:21:00Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Example(s) =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17205</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17205"/>
				<updated>2018-12-13T00:20:33Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Example =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17204</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17204"/>
				<updated>2018-12-13T00:16:48Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users :&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17203</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17203"/>
				<updated>2018-12-13T00:16:09Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17202</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17202"/>
				<updated>2018-12-13T00:13:32Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) In the future, after you login simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@laptop:~] ssh graham.sharcnet.ca&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17201</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17201"/>
				<updated>2018-12-13T00:12:26Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a (((ONE TIME))) required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time after logging in, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17200</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17200"/>
				<updated>2018-12-13T00:11:42Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time after logging in, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17199</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17199"/>
				<updated>2018-12-13T00:10:51Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module and atop will be in your path ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17198</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17198"/>
				<updated>2018-12-13T00:10:13Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Next install package &amp;quot;atop&amp;quot; or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17197</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17197"/>
				<updated>2018-12-13T00:09:30Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing a Package as a new user */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17196</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17196"/>
				<updated>2018-12-13T00:09:21Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing a Package as a new user ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17195</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17195"/>
				<updated>2018-12-13T00:06:39Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
== Installing A Package As A New User ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17194</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17194"/>
				<updated>2018-12-13T00:05:37Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing A Package As A New User =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17193</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17193"/>
				<updated>2018-12-13T00:05:03Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing A Package As A New User Example =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, this is a ONE TIME required step for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17192</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17192"/>
				<updated>2018-12-13T00:04:03Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing A Package As A New User Example =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop AND remove nixpkgs to become just like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17191</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17191"/>
				<updated>2018-12-13T00:03:30Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing A Package As A New User Example =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop and remove nixpkgs to be like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17190</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17190"/>
				<updated>2018-12-13T00:03:11Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing A Package As A New User Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing A Package As A New User Example =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop and remove nixpkgs to be like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Finally install atop, or any other nix package for that matter !!!&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop ;)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17189</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17189"/>
				<updated>2018-12-13T00:01:58Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* SIMPLE EXAMPLE: Installing A Package As A New User */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing A Package As A New User Example =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop and remove nixpkgs to be like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17188</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17188"/>
				<updated>2018-12-13T00:01:07Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Using channels to obtain Nix expressions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= SIMPLE EXAMPLE: Installing A Package As A New User =&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop and remove nixpkgs to be like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17187</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17187"/>
				<updated>2018-12-12T23:59:31Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Reinstalling atop Package AND nixpkgs Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing A Package As A New User == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop and remove nixpkgs to be like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17186</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17186"/>
				<updated>2018-12-12T23:51:04Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Reinstalling atop Package AND nix Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Reinstalling atop Package AND nixpkgs Channel Example == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets uninstall atop and remove nixpkgs to be like a new user ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time you login, simply load the nix module to use atop !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17185</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17185"/>
				<updated>2018-12-12T23:48:58Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Reinstalling atop Package AND nix Channel Example == &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets clear the slate and uninstall atop and nixpkgs for this example ...&lt;br /&gt;
If you have never used nix before on the system this step is unnecessary then goto step 2)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Lets now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time we login, to use atop simply load the nix module !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17184</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17184"/>
				<updated>2018-12-12T23:47:45Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets clear the slate and uninstall atop and nixpkgs for this example ...&lt;br /&gt;
If you have never used nix before on the system this step is unnecessary then goto step 2)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Lets now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time we login, to use atop simply load the nix module !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17183</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17183"/>
				<updated>2018-12-12T23:47:07Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets clear the slate and uninstall atop and nixpkgs for this example ...&lt;br /&gt;
If you have never used nix before on the system this step is unnecessary then goto step 2)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Lets now install nixpkgs, which is a ONE TIME required operation for new nix users ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
&lt;br /&gt;
3) Now install atop, or any other nix package for that matter ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time we login, to use atop simply load the nix module !&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17182</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17182"/>
				<updated>2018-12-12T23:45:36Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1) Lets clear the slate and uninstall atop and nixpkgs for this example.&amp;lt;br&amp;gt;&lt;br /&gt;
If you have never used nix before on the system this step is unnecessary then goto step 2)&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Lets now install nixpkgs, which is a ONE TIME required operation ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
&lt;br /&gt;
3) And lets install atop as an example, or any other nix package ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time we login atop will be available in our path once the nix module is loaded ....&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17181</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17181"/>
				<updated>2018-12-12T23:42:52Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
1) Lets clear the slate and uninstall atop and nixpkgs for this example.&amp;lt;br&amp;gt;&lt;br /&gt;
If you have never used nix before on the system this step is unnecessary then goto step 2)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
atop-2.3.0&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --query&lt;br /&gt;
nix-index-0.1.0&lt;br /&gt;
&lt;br /&gt;
2) Lets now install nixpkgs, which is a ONE TIME required operation ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
&lt;br /&gt;
3) And lets install atop as an example, or any other nix package ...&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4) Next time we login atop will be available in our path once the nix module is loaded ....&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] which atop&lt;br /&gt;
~/.nix-profile/bin/atop&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17180</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17180"/>
				<updated>2018-12-12T23:30:05Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
&lt;br /&gt;
Logout and login again ....&lt;br /&gt;
&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17179</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17179"/>
				<updated>2018-12-12T23:28:36Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17178</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17178"/>
				<updated>2018-12-12T23:27:55Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Remove/Read Package &amp;amp; Channel Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
failed to lock thread to CPU 8&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install atop&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17177</id>
		<title>NIX</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=NIX&amp;diff=17177"/>
				<updated>2018-12-12T23:25:56Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* Installing packages from a channel */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=NIX&lt;br /&gt;
|package_description=User Level Purely Functional Package Manager&lt;br /&gt;
|package_idnumber=171&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
Nix is a package manager system that allows users to manage their own software environment.  This environment is persistent and is shared across all clusters.&lt;br /&gt;
&lt;br /&gt;
* Users can build, install, upgrade, downgrade, and remove packages from their environment without root privileges and without affecting other users.&lt;br /&gt;
* Operations either succeed and create a new environment or fail leaving the previous environment in place (operations are atomic).&lt;br /&gt;
* Previous environments can be switched back to at any point.&lt;br /&gt;
* Users can add their own packages and share them with other users.&lt;br /&gt;
&lt;br /&gt;
The default Nix package set includes a huge selection (over 10,000) of recent versions of many packages.&lt;br /&gt;
== Enabling and disabling the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
The user's current Nix environment is enabled by loading the nix module. This creates some ''.nix*'' files and sets some environment variables.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is disabled by unloading the nix module. This unsets the environment variables but leaves the ''.nix*'' files alone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
ls -ld .nix*&lt;br /&gt;
env | fgrep -i nix&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Completely reseting the Nix environment ==&lt;br /&gt;
&lt;br /&gt;
Most operations can be undone with the &amp;lt;code&amp;gt;--rollback&amp;lt;/code&amp;gt; option (i.e.,  &amp;lt;code&amp;gt;nix-env --rollback&amp;lt;/code&amp;gt; or  &amp;lt;code&amp;gt;nix-channel --rollback&amp;lt;/code&amp;gt;). Sometimes it is useful to entirely reset nix though. This is done by unloading the module, erasing all user related nix files, and then reloading the module file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module unload nix&lt;br /&gt;
rm -fr ~/.nix-profile ~/.nix-defexpr ~/.nix-channels ~/.config/nixpkgs&lt;br /&gt;
rm -fr /nix/var/nix/profiles/per-user/$USER /nix/var/nix/gcroots/per-user/$USER&lt;br /&gt;
module load nix&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Installing and remove packages =&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; command is used to setup your Nix environment.&lt;br /&gt;
== What do I have installed and what can I install ==&lt;br /&gt;
&lt;br /&gt;
Lets first see what we currently have installed.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now let's see what is available. We request the attribute paths (unambiguous way of specifying a package) and the descriptions too (cursor to the right to see them). This takes a bit of time as it visits a lot of small files. Especially over NFS it can be a good idea to pipe it to a file and then grep that in the future.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages ==&lt;br /&gt;
&lt;br /&gt;
Let's say that we need a newer version of git than provided by default on our OS (e.g., the CentOS 6 one has issues with fetching over https). First lets check what our OS comes with.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's tell Nix to install its version in our environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.git&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's checkout what we have now (it may be necessary to tell bash to to forget remembered executable locations with &amp;lt;code&amp;gt;hash -r&amp;lt;/code&amp;gt; so it notices the new one).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;git --version&lt;br /&gt;
which git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Removing packages ==&lt;br /&gt;
&lt;br /&gt;
For completeness, lets add in the other usual version-control suspects.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs.subversion nixpkgs.mercurial&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Actually, we probably don't really want subversion any more. Let's remove that.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --uninstall subversion&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Environments =&lt;br /&gt;
&lt;br /&gt;
Nix keeps referring to user environments. Each time we install or remove packages we create a new environment based off of the previous environment.&lt;br /&gt;
== Switching between previous environments ==&lt;br /&gt;
&lt;br /&gt;
This means the previous environments still exist and we can switch back to them at any point. Let's say we changed our mind and want subversion back. It's trivial to restore the previous environment.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Of course we may want to do more than just move to the previous environment. We can get a list of all our environments so far and then jump directly to whatever one we want. Let's undo the rollback.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 4&lt;br /&gt;
nix-env --query&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Operations are atomic ==&lt;br /&gt;
&lt;br /&gt;
Due to the atomic property of Nix environments, we can't be left halfway through installing/updating packages. They either succeed and create us a new environment or leave us with the previous one intact.&lt;br /&gt;
&lt;br /&gt;
Let's go back to the start when we just had Nix itself and install the one true GNU distributed version control system tla. Don't let it complete though. Hit it with &amp;lt;code&amp;gt;CTRL+c&amp;lt;/code&amp;gt; partway through.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --switch-generation 1&lt;br /&gt;
nix-env --install --attr nixpkgs.tla&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+c&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Nothing bad happens. The operation didn't complete so it has no effect on the environment whatsoever.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Nix only does things once ==&lt;br /&gt;
&lt;br /&gt;
The install and remove commands take the current environment and create a new environment with the changes. This works regardless of which environment we are currently in. Let's create a new environment from our original environment by just adding git and mercurial.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --list-generations&lt;br /&gt;
nix-env --install nixpkgs.git nixpkgs.mercurial&lt;br /&gt;
nix-env --list-generations&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Notice how much much faster it was to install git and mercurial the second time? That is because the software already existed in the local Nix store from the previous installs so Nix just reused it.&lt;br /&gt;
== Garbage collection ==&lt;br /&gt;
&lt;br /&gt;
Nix periodically goes through and removes any software not accessible from any existing environments. This means we have to explicitly delete environments we don't want anymore so Nix is able to reclaim the space. We can delete specific environments or any sufficiently old.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --delete-generations 30d&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Using channels to obtain Nix expressions =&lt;br /&gt;
&lt;br /&gt;
Nix packages are Nix expressions that specify how to build something. The default source for of these expression for &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; is ''~/.nix-defexp'' and ''~/.nix-defexp/channels''. Nix channels provide a way to populate this later directory with existing expressions from repositories on the internet.&lt;br /&gt;
== Subscribing to a channel ==&lt;br /&gt;
&lt;br /&gt;
By default we are subscribed to the SHARCNET version of the latest NixOS release under the name nixpkg. Let us add the older NixOS 15.09 release too under the name nixpkgs_old and then download the latest versions of all NixOS 15.09 the expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --list&lt;br /&gt;
nix-channel --add file:///nix/channels/sharcnet-15.09 nixpkgs_old&lt;br /&gt;
nix-channel --list&lt;br /&gt;
nix-channel --update nixpkgs_old&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Installing packages from a channel ==&lt;br /&gt;
&lt;br /&gt;
Now searching our available packages we see that we can install software using either the nixpkgs or nixpkgs_old expressions.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --query --available --attr-path --description git&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Let's replace our git built and installed from the unstable (nixpkgs) expression with one built and installed from the stable expression (note that git is the default version of git which is gitMinimal).&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr nixpkgs_old.git&lt;br /&gt;
nix-env --query&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remove/Read Package &amp;amp; Channel Example== &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --uninstall atop&lt;br /&gt;
uninstalling 'atop-2.3.0'&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --remove nixpkgs&lt;br /&gt;
uninstalling 'nixpkgs'&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --add file:///nix/channels/ccpkgs/latest nixpkgs&lt;br /&gt;
[roberpj@gra-login1:~] nix-channel --update&lt;br /&gt;
failed to lock thread to CPU 8&lt;br /&gt;
unpacking channels...&lt;br /&gt;
[roberpj@gra-login1:~] module unload nix&lt;br /&gt;
[roberpj@gra-login1:~] module load nix&lt;br /&gt;
[roberpj@gra-login1:~] nix-env --install --attr nixpkgs.atop&lt;br /&gt;
replacing old 'atop-2.3.0'&lt;br /&gt;
installing 'atop-2.3.0'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What about dependency conflicts ==&lt;br /&gt;
&lt;br /&gt;
There are no issues with having a mix of packages installed from different sources even if they have conflicting dependencies. Nix puts each package in a separate directory under ''/nix/store'' versioned by the hash of the its build instructions. Binaries are linked with rpaths to ensure they always find the versions they need.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;ldd $(readlink ($which git))&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Switching between environments continue to work as before.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --rollback&lt;br /&gt;
git --version&lt;br /&gt;
nix-env --list-generations&lt;br /&gt;
nix-env --switch-generation 5&lt;br /&gt;
git --version&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== Switching between previous updates ==&lt;br /&gt;
&lt;br /&gt;
The channel updates (updating the list of expressions we can build and install from) are also atomic and versioned. This ensures we never find ourselves stuck due to accidentally updating to something broken.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-channel --rollback&lt;br /&gt;
nix-env --query --available --attr-path git&lt;br /&gt;
nix-channel --rollback 2&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
= Development with Nix (e.g., Python, R, etc.) =&lt;br /&gt;
&lt;br /&gt;
Several languages have their own repository of packages and associated infrastructure (e.g., PyPI and pip). Nix builds on these to automatically handle the external dependencies (e.g., C libraries) and makes it easy to work simultaneously with different projects requiring different versions of internal packages.&lt;br /&gt;
== Wrapper scripts for setting paths ==&lt;br /&gt;
&lt;br /&gt;
Nix has expressions for many of these that generate wrapper scripts that set path environment variables to bring a specific set of the internal packages into scope and then run the approriate program.&lt;br /&gt;
&lt;br /&gt;
The following Nix expression defines top-level myPython, myR, and myHaskell attributes that use the appropriate package-specific expressions to create wrappers for a selection of internal packages (for details about the Nix langauge see the [https://nixos.org/nix/manual/#ch-expression-language Nix Expression Language].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
rec {&lt;br /&gt;
  myPython = python3.buildEnv.override rec {&lt;br /&gt;
    extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myR = rWrapper.override rec {&lt;br /&gt;
    packages = with rPackages; [ rgeos rgdal ];&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  myHaskell = haskellPackages.ghcWithPackages (ghcpkgs:&lt;br /&gt;
    with ghcpkgs; [ pipes lens cabal-install ]&lt;br /&gt;
  );&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Saving it as ''~/.nix-defexpr/mypkgs.nix'' makes these available for installation with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-env --install --attr mypkgs.myPython&lt;br /&gt;
cat $(which python3)&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
== Development environments ==&lt;br /&gt;
&lt;br /&gt;
A Nix expression evaluates to a specification for setting up a (build) environment and then doing a build in it. Frequently we want to do something very similar: setup a (development) environment and then do development in it.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; command bridges the gap between these two. It uses Nix to sets up a non-rooted (build) environment with the dependencies we specify and then laucnhes a (development) shell in it instead of a build process.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;mkdir devel&lt;br /&gt;
cd devel&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Say we need Clang and Python with the NumPy and SciPy packages. Instead of installing these into our global environment we can create a Nix expression that specifies these as build (development) inputs&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    ( python3.buildEnv.override rec {&lt;br /&gt;
        extraLibs = with python3Packages; [ numpy scipy ];&lt;br /&gt;
      } )&lt;br /&gt;
    clang&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
save it in ''default.nix'' and then run &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;nix-shell&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
Now we are in a build (development) environment with all the dependencies we specified in the appropriate PATHs.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;cat $(which python3)&lt;br /&gt;
clang --version&lt;br /&gt;
python3&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;import scipy&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
To return to our standard environment we just exit the shell. This is extreamily nice if we work on many projects with conflicting dependencies.&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;exit&lt;br /&gt;
cd ..&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
== One-off environments ==&lt;br /&gt;
&lt;br /&gt;
Quite frequently we need a one-off development environment with a few packages.  Say CLang and git.  Rather than have to write the following boilerplate ''default.nix'' file&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import &amp;amp;lt;nixpkgs&amp;amp;gt; { };&lt;br /&gt;
&lt;br /&gt;
stdenv.mkDerivation {&lt;br /&gt;
  name = &amp;amp;quot;my-env&amp;amp;quot;;&lt;br /&gt;
  buildInputs = [&lt;br /&gt;
    clang&lt;br /&gt;
    git&lt;br /&gt;
  ];&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
we can just get &amp;lt;code&amp;gt;nix-shell&amp;lt;/code&amp;gt; to do it for us.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-shell --packages clang git&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;blockquote&amp;gt;CTRL+d&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Submitting Jobs =&lt;br /&gt;
&lt;br /&gt;
Loading the Nix module before submitting a job makes the Nix environment available to the job.  Note that these jobs will see the current Nix environment including any changes made after submission.  Compiled binaries should not require the Nix module to be loaded to run.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;module load nix&lt;br /&gt;
sqsub ...&amp;lt;/pre&amp;gt;&lt;br /&gt;
= Internals =&lt;br /&gt;
&lt;br /&gt;
This section details some of the internals of how Nix works and how to create your own packages.  It is more advanced material not required for the basic usage.&lt;br /&gt;
== The Nix store ==&lt;br /&gt;
&lt;br /&gt;
A Nix environment is just a collection of symlinks to all the packages that exist in that environment.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;readlink $(which git)&lt;br /&gt;
readlink -f ~/.nix-profile&amp;lt;/pre&amp;gt;&lt;br /&gt;
Everything in Nix exists as a path in the Nix store. Each path is distinguished with a hash of either its contents or everything that went into creating it to keep it separate from everything else. Paths are immutable once created. This means they can be freely shared.&lt;br /&gt;
&lt;br /&gt;
Paths are created by the Nix build server when it realizes a Nix derivation. Nix derivations specify all the inputs to a jailed process that creates the contents of the store path.&lt;br /&gt;
== Nix expressions and instantiation ==&lt;br /&gt;
&lt;br /&gt;
Nix derivations are instantiated from Nix expressions, which are written in the Nix language. The &amp;lt;code&amp;gt;nix-repl&amp;lt;/code&amp;gt; program can be used to interactively experiment with the Nix language.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nix-repl&lt;br /&gt;
nix-repl&amp;lt;/pre&amp;gt;&lt;br /&gt;
The following ''cabextract.nix'' Nix expression evaluates to a derivation fully specifying how to build the &amp;lt;code&amp;gt;cabextract&amp;lt;/code&amp;gt; utility.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;with import ~/.nix-defexpr/nixpkgs { };&lt;br /&gt;
stdenv.mkDerivation rec {&lt;br /&gt;
  name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
  src = fetchurl {&lt;br /&gt;
    url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
    sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
  meta = with stdenv.lib; {&lt;br /&gt;
    homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
    description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
    platforms = platforms.all;&lt;br /&gt;
    license = licenses.gpl3;&lt;br /&gt;
    maintainers = with maintainers; [ pSub ];&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is written using the &amp;lt;code&amp;gt;stdenv.mkDerivation&amp;lt;/code&amp;gt; function from &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;. This function creates a derivation that executes the standard unpack, patch, configure, build, check, install, fixup, check, and distribute steps on the package. It provides a series of hooks that can be used at each step to customize the process for non-standard packages.  For full details see the [https://nixos.org/nixpkgs/manual nixpkgs manual].&lt;br /&gt;
&lt;br /&gt;
The Nix expression is instantiate to a derivation using &amp;lt;code&amp;gt;nix-instantiate&amp;lt;/code&amp;gt;. The leading ''./'' is required to distinguish that the arguments is file containing a Nix expression to be instantiated and not the name of a package in the default Nix expression to instantiate.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabdrv=$(nix-instantiate ./cabextract.nix)&lt;br /&gt;
echo $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Nix derivations and realization ==&lt;br /&gt;
&lt;br /&gt;
The Nix derivation instantiated from the above Nix expression can be pretty-printed using the &amp;lt;code&amp;gt;pp-aterm&amp;lt;/code&amp;gt; program.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr nixpkgs.strategoPackages.strategoxt&lt;br /&gt;
pp-aterm -i $cabdrv&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;Derive(&lt;br /&gt;
  [(&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)]&lt;br /&gt;
, [ (&amp;amp;quot;/home/nixbld/store/...-stdenv.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  , (&amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42.drv&amp;amp;quot;, [&amp;amp;quot;out&amp;amp;quot;])&lt;br /&gt;
  ]&lt;br /&gt;
, [&amp;amp;quot;/home/nixbld/store/...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, &amp;amp;quot;x86_64-linux&amp;amp;quot;&lt;br /&gt;
, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;&lt;br /&gt;
, [&amp;amp;quot;-e&amp;amp;quot;, &amp;amp;quot;...-default-builder.sh&amp;amp;quot;]&lt;br /&gt;
, [ (&amp;amp;quot;buildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;builder&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-bash-4.3-p42/bin/bash&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;name&amp;amp;quot;, &amp;amp;quot;cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;nativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;out&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;propagatedNativeBuildInputs&amp;amp;quot;, &amp;amp;quot;&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;src&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-cabextract-1.6.tar.gz&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;stdenv&amp;amp;quot;, &amp;amp;quot;/home/nixbld/store/...-stdenv&amp;amp;quot;)&lt;br /&gt;
  , (&amp;amp;quot;system&amp;amp;quot;, &amp;amp;quot;x86_64-linux&amp;amp;quot;)&lt;br /&gt;
  ]&lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Nix build server realizes derivations by building the packages in an isolated environment. Each derivation specifies what comes out of the environment, what goes into it, the required machine architecture, what build program to execute in the environment, and what arguments and environment to pass to the program to be executed.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program &amp;lt;code&amp;gt;--realize&amp;lt;/code&amp;gt; option is used to signal the build server to realize the derivation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;cabpath=$(nix-store --realize $cabdrv)&lt;br /&gt;
echo $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; program will add the realized derivation to the users environment with the &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-i&amp;lt;/code&amp;gt;) option. Technically it creates a new realization of the &amp;lt;code&amp;gt;user-environment&amp;lt;/code&amp;gt; package that symlinks in the entire contents of the path path and switches to it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install $cabpath&amp;lt;/pre&amp;gt;&lt;br /&gt;
The build log associated with a realization can be viewed with the &amp;lt;code&amp;gt;--read-log&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-l&amp;lt;/code&amp;gt;) option.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --read-log $cabdrv&lt;br /&gt;
nix-store --read-log $cabpath&lt;br /&gt;
nix-store --read-log $(which git)&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-store&amp;lt;/code&amp;gt; program also supports numerous other store related items such as computing the transitive closure of a package with the &amp;lt;code&amp;gt;--query&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-q&amp;lt;/code&amp;gt;) &amp;lt;code&amp;gt;--requisites&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;-R&amp;lt;/code&amp;gt;) and exporting them via the &amp;lt;code&amp;gt;--export&amp;lt;/code&amp;gt; option to a Nix archive for import into another store.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-store --query --requisites $cabpath&lt;br /&gt;
nix-store --export $(nix-store --query --requisites $(which git)) | gzip &amp;amp;gt; git.nar.gz&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Nix default expression ==&lt;br /&gt;
&lt;br /&gt;
Nix has a default expression created from the contents of ''~/.nix-defexpr''. The &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;--install&amp;lt;/code&amp;gt; operation normally works by selecting an attribute in this expression (fast) or matching against the &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; attribute in all the attributes in this expression (slow) to determine an expression to instantiate, realize, and then symlink into a new environment.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;nix-channel&amp;lt;/code&amp;gt; command works by building new Nix expression packages (containing all the Nix expressions for the subscribed channels) and symlinking ''~/.nix-defexprs/channels'' to then. Adding additional expressions to ''~/.nix-defexpr'' makes them available for use with &amp;lt;code&amp;gt;nix-env&amp;lt;/code&amp;gt; as well. For example, the following ''~/.nix-defexpr/mypkgs'' expression packages up the above ''cabextract.nix'' example.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;nix&amp;quot;&amp;gt;args@{ ... }: with import ./nixpkgs args;&lt;br /&gt;
rec {&lt;br /&gt;
  cabextract = stdenv.mkDerivation rec {&lt;br /&gt;
    name = &amp;amp;quot;cabextract-1.6&amp;amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    src = fetchurl {&lt;br /&gt;
      url = &amp;amp;quot;http://www.cabextract.org.uk/${name}.tar.gz&amp;amp;quot;;&lt;br /&gt;
      sha256 = &amp;amp;quot;1ysmmz25fjghq7mxb2anyyvr1ljxqxzi4piwjhk0sdamcnsn3rnf&amp;amp;quot;;&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    meta = with stdenv.lib; {&lt;br /&gt;
      homepage = http://www.cabextract.org.uk/;&lt;br /&gt;
      description = &amp;amp;quot;Free Software for extracting Microsoft cabinet files&amp;amp;quot;;&lt;br /&gt;
      platforms = platforms.all;&lt;br /&gt;
      license = licenses.gpl3;&lt;br /&gt;
      maintainers = with maintainers; [ pSub ];&lt;br /&gt;
    };&lt;br /&gt;
  };&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
This makes it possible to install packages from &amp;lt;code&amp;gt;mypkgs&amp;lt;/code&amp;gt; as easily as those from the official &amp;lt;code&amp;gt;nixpkgs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;sh&amp;quot;&amp;gt;nix-env --install --attr mypkgs.cabextract&amp;lt;/pre&amp;gt;&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o Nix Package Manager&amp;lt;br&amp;gt;&lt;br /&gt;
http://nixos.org/nix/&lt;br /&gt;
&lt;br /&gt;
o Official Nix/Nixpkgs/NixOS&amp;lt;br&amp;gt;&lt;br /&gt;
https://github.com/NixOS&lt;br /&gt;
&lt;br /&gt;
o Nix on SHARCNET (slides by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.sharcnet.ca/help/images/5/5f/Tyson_nix_2015.pdf&lt;br /&gt;
&lt;br /&gt;
o Exploring a new approach to package management (youtube video by Tyson Whitehead)&amp;lt;br&amp;gt;&lt;br /&gt;
https://www.youtube.com/watch?v=pQE9WTLAPHQ&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17169</id>
		<title>LSDYNA</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17169"/>
				<updated>2018-12-06T21:24:32Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* MPI Job */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=LSDYNA&lt;br /&gt;
|package_description=Suite of programs for transient dynamic finite element program&lt;br /&gt;
|package_idnumber=104&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Before a research group can use LSDYNA on sharcnet, a license must be purchased directly from LSTC for the sharcnet license server.  Alternately, if a research group resides at a institution that has sharcnet computing hardware (mac, uwo, guelph, waterloo) it maybe possible to use a pre-existing site license hosted on an accessible institutional license server.   To access and use this software you must open a ticket and request to be added to the sharcnet &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt; group.&lt;br /&gt;
&lt;br /&gt;
=Version Selection=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Create and customize this file for your license server, where XXXXX should be replaced with your port number and Y with your server number:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra1:~] cat .licenses/ls-dyna.lic &lt;br /&gt;
 #LICENSE_TYPE: network&lt;br /&gt;
 #LICENSE_SERVER: XXXXX@licenseY.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The modules are loaded automatically in the testomp.sh and testmpi.sh script shown below in the example section:&lt;br /&gt;
&lt;br /&gt;
For single node smp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna&lt;br /&gt;
        ls-dyna/7.1.2&lt;br /&gt;
        ls-dyna/7.1.3&lt;br /&gt;
        ls-dyna/8.1&lt;br /&gt;
        ls-dyna/9.1&lt;br /&gt;
        ls-dyna/9.2&lt;br /&gt;
        ls-dyna/10.0&lt;br /&gt;
&lt;br /&gt;
To load any version do for example:&lt;br /&gt;
&lt;br /&gt;
 module load ls-dyna/9.1&lt;br /&gt;
&lt;br /&gt;
For multi node mpp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna-mpi&lt;br /&gt;
        ls-dyna-mpi/7.1.2&lt;br /&gt;
        ls-dyna-mpi/7.1.3&lt;br /&gt;
        ls-dyna-mpi/8.1&lt;br /&gt;
        ls-dyna-mpi/9.1&lt;br /&gt;
        ls-dyna-mpi/9.2&lt;br /&gt;
        ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
To load the 7.1.X modules do the following:&lt;br /&gt;
 module load openmpi/1.6.5 ls-dyna-mpi/7.1.3&lt;br /&gt;
&lt;br /&gt;
To load the 8.X or 9.X modules do:&lt;br /&gt;
 module load openmpi/1.8.8 ls-dyna-mpi/9.2&lt;br /&gt;
&lt;br /&gt;
To load the 10.X modules do:&lt;br /&gt;
 module load openmpi/1.10.7 ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Once a license configuration is established, the research group will be given a 5 digit port number.  The value should then be inserted into the appropriate departmental export statement before loading the module file as follows:&lt;br /&gt;
&lt;br /&gt;
o UofT Mechanical Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o McGill Mechanical Engineering Department&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license2.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o UW Mechanical and Mechatronics Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license3.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Laurentian University Bharti School of Engineering&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license4.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Fictitious Example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;12345&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The next step is load the desired sharcnet lsdyna module version.  Check which modules are available by running the &amp;lt;i&amp;gt;module avail&amp;lt;/i&amp;gt; command then load one the modules as shown:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] module avail lsdyna&lt;br /&gt;
------------------------------------ /opt/sharcnet/modules ------------------------------------&lt;br /&gt;
lsdyna/hyb/r711.88920     lsdyna/mpp/r711.88920     lsdyna/smp/r611.79036&lt;br /&gt;
lsdyna/mpp/ls971.r85718   lsdyna/mpp/r712.95028     lsdyna/smp/r711.88920&lt;br /&gt;
lsdyna/mpp/ls980B1.011113 lsdyna/mpp/r800.95359     lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna/mpp/r611.79036     lsdyna/mpp/r901.109912    lsdyna/smp/r800.95359&lt;br /&gt;
lsdyna/mpp/r611.80542     lsdyna/smp/ls980B1.78258  lsdyna/smp/r901.109912&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r9.0.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r901.109912&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r901.109912&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r8.0.0 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r800.95359&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r800.95359&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.2 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r712.95028&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/mpp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r6.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r611.79036&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
or&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.80542&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;i&amp;gt;r611.80542&amp;lt;/i&amp;gt; provides lsdyna_s only.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====ls980 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/ls980B1.78258&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.4.5 lsdyna/mpp/ls980B1.011113&lt;br /&gt;
or&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/smp/ls980B1.78258&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note1) restart capability for ls980smpB1 and ls980mppB1 is not supported&lt;br /&gt;
Note2) the module using legacy openmpi/intel/1.4.5 will run extremely slow&lt;br /&gt;
&lt;br /&gt;
=Job Submission=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
The submission scripts myompjob.sh and mympijob.sh for the airbag problem are shown in the &amp;lt;b&amp;gt;Example Job&amp;lt;/b&amp;gt; section below, for both graham and orca.  Please note that you should specify your own username if submitting to a def account (not roberpj).  Alternatively you could specify your resource allocation account:&lt;br /&gt;
&lt;br /&gt;
Sample threaded job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch myompjob.sh&lt;br /&gt;
&lt;br /&gt;
Sample mpi job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch  mympijob.sh&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
When loading a lsdyna sharcnet legacy module on the new orca or a sharcnet legacy system, the single or double precision solvers are specified with &amp;lt;strong&amp;gt;lsdyna_s&amp;lt;/strong&amp;gt; or &amp;lt;strong&amp;gt;lsdyna_d&amp;lt;/strong&amp;gt; respectively, as shown in the following sqsub commands:&lt;br /&gt;
&lt;br /&gt;
===1cpu SERIAL Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q serial -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===4cpu SMP Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=1G lsdyna_s i=airbag.deploy.k ncpu=4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using an explicit solver, one can specify a conservative initial memory setting on the command line as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=4 memory=754414&amp;lt;/pre&amp;gt;&lt;br /&gt;
where memory is the minimum number of 8 byte words shared by all processors in double precision, or, 4 byte words in single precision.&lt;br /&gt;
&lt;br /&gt;
The initial value can be determined by starting a simulation interactively on the command line, and finding the output resembling: &amp;lt;i&amp;gt; Memory required to begin solution      :      754414&amp;lt;/i&amp;gt;.    The number of words can be specified as memory=260M instead of memory=260000000,  for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  &lt;br /&gt;
&lt;br /&gt;
===8cpu MPI Job===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The initial memory can also be specified for mpi jobs  on the sqsub command line with &amp;quot;memory=&amp;quot; for the first master processor to decompose the problem  and &amp;quot;memory2=&amp;quot; used on all processors including the master to solves the decomposed problem, where the values are specified as 4 bytes per word in single precision and 8 bytes per word in double precision.   The number of words can be specified as memory=260M instead of memory=260000000 OR memory2=260M instead of memory2=260000000 for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  The initial values can be found by running simulation interactively on a orca compute node and checking the output for a line such as: &amp;lt;i&amp;gt;Memory required to begin solution (memory=     464898  memory2=     158794 )&amp;lt;/i&amp;gt; which could then be implemented for a job run in the queue by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898 memory2=158794&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The specification of memory2 on sharcnet or compute canada clusters is not beneficial since the queue reserves the same memory per core for all nodes, such that the decomposition master node process cannot be allocated to have larger system memory.  Therefore its sufficient to specify a single memory parameter by doing:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto &lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the LSTC_MEMORY variable will only allow the memory to grow for explicit simulations. The following slurm examples demonstrate how to prescribe the memory parameters exactly.&lt;br /&gt;
&lt;br /&gt;
=Example Job=&lt;br /&gt;
&lt;br /&gt;
Copy the airbag example to your account:&lt;br /&gt;
&lt;br /&gt;
 rsync -av orca.computecanada.ca:/opt/sharcnet/lsdyna/r901.109912/examples r901.109912_examples&lt;br /&gt;
 cd r901.109912_examples/examples/misc/airbag&lt;br /&gt;
 gunzip airbag.deploy.k.gz&lt;br /&gt;
&lt;br /&gt;
==Graham (default modules)==&lt;br /&gt;
&lt;br /&gt;
Please note that graham does not have the sharcnet legacy modules installed on it.&lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mysmpjob1.sh&amp;lt;/b&amp;gt; for 4 core single precision smp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=4                   # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testsmp1                 # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load ls-dyna/9.1&lt;br /&gt;
ls-dyna_s i=airbag.deploy.k ncpu=4 memory=1000M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 4 (bytes/word) = 1000M words&lt;br /&gt;
&lt;br /&gt;
===Mpi Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mympijob2.sh&amp;lt;/b&amp;gt; for 4 core double precision mpp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --ntasks=4                     # number of ranks&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M            # memory per rank&lt;br /&gt;
#SBATCH --time=00:30:00                # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testmpi2            # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load openmpi/1.8.8 ls-dyna-mpi/9.1&lt;br /&gt;
srun ls-dyna_d i=airbag.deploy.k memory=500M memory2=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
==New Orca (legacy modules)==&lt;br /&gt;
&lt;br /&gt;
Please note, while new orca has most compute canada cvmfs modules available by default it does not have the graham the &amp;lt;i&amp;gt;ls-dyna&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;ls-dyna-mpi&amp;lt;/i&amp;gt; modules installed.  Therefore currently the only way to run lsdyna is by using the legacy sharcnet &amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt; modules as shown in the following two smp and mpi examples. &lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Submission script &amp;lt;b&amp;gt;mysmpjob3.sh&amp;lt;/b&amp;gt; to run 16 core single precision single node smp job with sharcnet legacy modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=16                  # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=mysmpjob3-1node-16core    # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server for legacy modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=31047@license3.sharcnet.ca&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does NOT work with legacy modules&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna_s i=airbag.deploy.k ncpu=$SLURM_CPUS_PER_TASK memory=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
===MPI Job ===&lt;br /&gt;
&lt;br /&gt;
To run the sharcnet legacy &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; efficiently over several compute nodes with many cores requires using orca's xeon nodes as shown in &amp;lt;b&amp;gt; mympijob4.sh&amp;lt;/b&amp;gt; below.   To run lsdyna on orca's opteron nodes with &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; requires using only &amp;lt;b&amp;gt;one&amp;lt;/b&amp;gt; opteron node as shown in &amp;lt;b&amp;gt; mympijob5.sh&amp;lt;/b&amp;gt; below.  The other legacy sharcnet lsdyna mpp modules, as shown in the Availability Table  lsdyna/mpp/ls971.r85718, lsdyna/mpp/ls980B1.011113, lsdyna/mpp/r611.79036, lsdyna/mpp/r611.80542, lsdyna/mpp/r711.88920, lsdyna/mpp/r800.95359 and lsdyna/mpp/r901.109912 have yet to be tested. If you want to use one but cannot get it working please open a ticket.  Note that lines containing a space between &amp;quot;# and SBATCH&amp;quot; are comments.  To activate such line(s) required removal of the space ie) &amp;quot;#SBATCH&amp;quot;.  Its recommended to use &amp;lt;i&amp;gt;module ls-dyna-mpi&amp;lt;/i&amp;gt; on graham instead since the machine is far newer, faster and reliable. &lt;br /&gt;
&lt;br /&gt;
====Multi Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob4.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=xeon&lt;br /&gt;
#SBATCH --ntasks=48&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob4-Xnodes-48cores-xeon  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out             # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE --mca plm_slurm_args '--cpu_bind=none' lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob5.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=opteron&lt;br /&gt;
#SBATCH --nodes=1&lt;br /&gt;
# SBATCH --ntasks=24&lt;br /&gt;
# SBATCH --mem=0&lt;br /&gt;
#SBATCH --ntasks=16&lt;br /&gt;
#SBATCH --mem-per-cpu=1000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob5-1node-16cores-opteron  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 1000M / 4 (bytes/word) = 250M words&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
STEP1) The following shows sqsub submission of the [http://www.dynaexamples.com/examples-manual/misc/airbag airbag] example to the mpi queue.  Its recommended to first edit  &amp;lt;i&amp;gt;airbag.deploy.k&amp;lt;/i&amp;gt;  and change &amp;lt;i&amp;gt;endtim&amp;lt;/i&amp;gt; to 3.000E-00 so the job runs long enough to perform the restart in steps 2 and 3 below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp -a /opt/sharcnet/lsdyna/r611.79036/examples /scratch/$USER/test-lsdyna&lt;br /&gt;
cd /scratch/$USER/test-lsdyna/misc/airbag&lt;br /&gt;
gunzip airbag.deploy.k.gz&lt;br /&gt;
cp airbag.deploy.k airbag.deploy.restart.k&lt;br /&gt;
nano deploy.restart.k (reduce plot file creation frequency)&lt;br /&gt;
   *DATABASE_BINARY_D3PLOT&lt;br /&gt;
   $       dt      lcdtdeploy.restart.k&lt;br /&gt;
   1.000E-01   &amp;lt;--- change from 5.000E-04 &lt;br /&gt;
export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
sqsub -r 10m -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP2) With the job still running, use the echo command as follows to create a file called &amp;quot;D3KIL&amp;quot; that will trigger generation of restart files at which point the file D3KIL itself will be erased.  Do this once a day if data loss is critical to you OR once or twice just before the  &amp;lt;i&amp;gt;sqsub -r&amp;lt;/i&amp;gt; time limit is reached.  Further information can be found here http://www.dynasupport.com/tutorial/ls-dyna-users-guide/sense-switch-control:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
echo &amp;quot;sw3&amp;quot; &amp;gt; D3KIL&lt;br /&gt;
sqkill job#&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP3) Before the job can be restarted, the following two lines must be added to the &amp;lt;i&amp;gt;airbag.deploy.restart.k&amp;lt;/i&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*CHANGE_CURVE_DEFINITION&lt;br /&gt;
         1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP4) Now resubmit the job as follows using &amp;quot;r=&amp;quot; to specify the restart file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sqsub -r 1h -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.restart.k r=d3dump01&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=General Notes=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Command Line Use==&lt;br /&gt;
&lt;br /&gt;
Parallel lsdyna jobs can be run interactively on the command line (outside the queue) for short run testing purposes as follows:&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node MPP (mpi) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/mpp/r711.88920&lt;br /&gt;
 mpirun -np 8 lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node SMP (threaded) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load lsdyna/smp/r711.88920&lt;br /&gt;
 lsdyna_d i=airbag.deploy.k ncpu=8&lt;br /&gt;
&lt;br /&gt;
==Memory Issues==&lt;br /&gt;
&lt;br /&gt;
A minumum of mpp=2G is recommended although the memory requirement of a job may suggest much less is required. For instance setting mpp=1G for the airbag test job above will result in the following error when running a job in the queue:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
An attempt to set processor affinity has failed - please check to&lt;br /&gt;
ensure that your system supports such functionality. If so, then&lt;br /&gt;
this is probably something that should be reported to the OMPI developers.&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another error message which can occur after a job runs for a while if mpp is chosen to small is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Process 45 &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Signal 11 : Segmentation Violation &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To get an idea of the amount of memory a job used run grep on the output file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep Memory&lt;br /&gt;
     |    Distributed Memory Parallel                  |&lt;br /&gt;
 Memory size from command line:      500000,       500000&lt;br /&gt;
 Memory for the head node&lt;br /&gt;
 Memory installed (MB)        :        32237&lt;br /&gt;
 Memory free (MB)             :        11259&lt;br /&gt;
 Memory required (MB)         :            0&lt;br /&gt;
 Memory required to process keyword     :       458120&lt;br /&gt;
 Memory required for decomposition      :       458120&lt;br /&gt;
 Memory required to begin solution (memory=      458120 memory2=      230721)&lt;br /&gt;
 Max. Memory reqd for implicit sol: max used               0&lt;br /&gt;
 Max. Memory reqd for implicit sol: incore                 0&lt;br /&gt;
 Max. Memory reqd for implicit sol: oocore                 0&lt;br /&gt;
 Memory required to complete solution (memory=      458120 memory2=      230721)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Version and Revision==&lt;br /&gt;
&lt;br /&gt;
Again run grep on the output file to extract the major and minor revision:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep 'Revision\|Version'&lt;br /&gt;
     |  Version : mpp s R6.1.1    Date: 01/02/2013     |&lt;br /&gt;
     |  Revision: 78769           Time: 07:43:30       |&lt;br /&gt;
     |  SVN Version: 80542                             |&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Check License Status==&lt;br /&gt;
&lt;br /&gt;
===Running and Queued Programs===&lt;br /&gt;
&lt;br /&gt;
To get a summary of running and/or queued jobs use the &amp;lt;i&amp;gt;lstc_qrun&amp;lt;/i&amp;gt; command as follows, where queued means the job has started running according to the sqsub command but its actually sitting waiting for license resources to come available.  Once these are acquired the job will start running on the cluster and appear as a Running Program according to  lstc_qrun ie)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
                     Running Programs&lt;br /&gt;
    User             Host          Program              Started       # procs&lt;br /&gt;
-----------------------------------------------------------------------------&lt;br /&gt;
dgierczy    20277@saw61.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dgierczy     8570@saw32.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dscronin    25486@hnd6             MPPDYNA_971      Tue Apr  9 20:19     6&lt;br /&gt;
dscronin    14897@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    14971@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    15046@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    31237@hnd16            MPPDYNA_971      Tue Apr  9 21:53     6&lt;br /&gt;
dscronin    31313@hnd16            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
dscronin     6396@hnd15            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
csharson    28890@saw175.saw.sharc MPPDYNA_971      Wed Apr 10 16:48     6&lt;br /&gt;
No programs queued&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To show license file expiration details, append &amp;quot;-R&amp;quot; to the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun -R&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
**** LICENSE INFORMATION ****&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-DYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dgierczy   20277@saw61.saw.sharcnet   1&lt;br /&gt;
 dgierczy    8570@saw32.saw.sharcnet   1&lt;br /&gt;
MPPDYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dscronin   25486@hnd6               6&lt;br /&gt;
 dscronin   14897@hnd18              6&lt;br /&gt;
 dscronin   14971@hnd18              6&lt;br /&gt;
 dscronin   15046@hnd18              6&lt;br /&gt;
 dscronin   31237@hnd16              6&lt;br /&gt;
 dscronin   31313@hnd16              6&lt;br /&gt;
 dscronin    6396@hnd15              6&lt;br /&gt;
 csharson   28890@saw175.saw.sharcnet   6&lt;br /&gt;
                   LICENSE GROUP    58    966   1024 |     0&lt;br /&gt;
&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-OPT           12/31/2013          0   1024   1024 |     0&lt;br /&gt;
                   LICENSE GROUP     0   1024   1024 |     0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Killling A Program===&lt;br /&gt;
&lt;br /&gt;
The queue normally kills lsdyna jobs cleanly.  However its possible that licences for a job (which is no longer running in the queue and therefore no longer has any processes running on the cluster) will continue to be tied up according to the lstc_qrun command.   To kill such a program determine the pid@hostname from the lstc_qrun command then run the following kill command.  The following example demonstrates the procedure for username roberpj:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
roberpj    45312@orc329.orca.shar MPPDYNA          Wed Dec  5 15:50    12&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~] lstc_qkill 45312@orc329.orca.sharcnet.ca&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
Program queued for termination&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
[roberpj@orc-login2:~] &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Legacy Instructions==&lt;br /&gt;
&lt;br /&gt;
The following binaries remain available on saw and orca for backward compatibility testing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] cd /opt/sharcnet/local/lsdyna&lt;br /&gt;
[roberpj@orc129:/opt/sharcnet/local/lsdyna] ls ls971*&lt;br /&gt;
ls971_d_R3_1    ls971_d_R4_2_1  ls971_s_R3_1    ls971_s_R4_2_1  ls971_s_R5_1_1&lt;br /&gt;
ls971_d_R4_2_0  ls971_d_R5_0    ls971_s_R4_2_0  ls971_s_R5_0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are currently no sharcnet modules for these versions, hence jobs should be submitted as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load lsdyna&lt;br /&gt;
export PATH=/opt/sharcnet/local/lsdyna:$PATH&lt;br /&gt;
export LSTC_LICENSE_SERVER=XXXXX@license3.uwo.sharcnet&lt;br /&gt;
cp /opt/sharcnet/local/lsdyna/examples/airbag.deploy.k airbag.deploy.k&lt;br /&gt;
SERIAL JOB:  sqsub -q serial -r 1d -o ofile.%J ls971_d_R4_2_1 i=airbag.deploy.k&lt;br /&gt;
THREADED JOB:  sqsub -q threaded -n 4 -r 1d -o ofile.%J ls971_d_R4_2_1 ncpu=4 para=2 i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note! the &amp;lt;i&amp;gt;ls971_s_R3_1&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;ls971_d_R3_1&amp;lt;/i&amp;gt; binaries do not work, a fix is being looked for.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Homepage&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/products/ls-dyna&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Support (Tutorials, HowTos, Faq, Manuals, Release Notes, News, Links)&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Release Notes&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/release-notes&lt;br /&gt;
&lt;br /&gt;
o LS-PrePost Online Documentation FAQ&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/lspp/content/faq.shtml&lt;br /&gt;
&lt;br /&gt;
o LSTC Download/Install Overview Page&amp;lt;br&amp;gt;&lt;br /&gt;
Provides links to binaries for LS-DYNA SMP/MPP, LS-OPT, LS-PREPOST, LS-TASC.&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/download&lt;br /&gt;
&lt;br /&gt;
==Memory Links==&lt;br /&gt;
&lt;br /&gt;
o Implicit: Memory notes&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.dynasupport.com/howtos/implicit/implicit-memory-notes&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Support Environment variables&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/howtos/general/environment-variables&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA and d3VIEW Blog (a few words on memory settings)&amp;lt;br&amp;gt;&lt;br /&gt;
http://blog2.d3view.com/a-few-words-on-memory-settings-in-ls-dyna/&lt;br /&gt;
&lt;br /&gt;
o Convert Words to GB ie) memory=500MW (3.73GB)&amp;lt;br&amp;gt;&lt;br /&gt;
http://deviceanalytics.com/memcalc.php&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	<entry>
		<id>https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17168</id>
		<title>LSDYNA</title>
		<link rel="alternate" type="text/html" href="https://www.sharcnet.ca/help/index.php?title=LSDYNA&amp;diff=17168"/>
				<updated>2018-12-06T21:21:00Z</updated>
		
		<summary type="html">&lt;p&gt;Roberpj: /* MPI Job */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Software&lt;br /&gt;
|package_name=LSDYNA&lt;br /&gt;
|package_description=Suite of programs for transient dynamic finite element program&lt;br /&gt;
|package_idnumber=104&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
Before a research group can use LSDYNA on sharcnet, a license must be purchased directly from LSTC for the sharcnet license server.  Alternately, if a research group resides at a institution that has sharcnet computing hardware (mac, uwo, guelph, waterloo) it maybe possible to use a pre-existing site license hosted on an accessible institutional license server.   To access and use this software you must open a ticket and request to be added to the sharcnet &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt;&amp;lt;/b&amp;gt; group.&lt;br /&gt;
&lt;br /&gt;
=Version Selection=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Create and customize this file for your license server, where XXXXX should be replaced with your port number and Y with your server number:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra1:~] cat .licenses/ls-dyna.lic &lt;br /&gt;
 #LICENSE_TYPE: network&lt;br /&gt;
 #LICENSE_SERVER: XXXXX@licenseY.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The modules are loaded automatically in the testomp.sh and testmpi.sh script shown below in the example section:&lt;br /&gt;
&lt;br /&gt;
For single node smp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna&lt;br /&gt;
        ls-dyna/7.1.2&lt;br /&gt;
        ls-dyna/7.1.3&lt;br /&gt;
        ls-dyna/8.1&lt;br /&gt;
        ls-dyna/9.1&lt;br /&gt;
        ls-dyna/9.2&lt;br /&gt;
        ls-dyna/10.0&lt;br /&gt;
&lt;br /&gt;
To load any version do for example:&lt;br /&gt;
&lt;br /&gt;
 module load ls-dyna/9.1&lt;br /&gt;
&lt;br /&gt;
For multi node mpp jobs the available versions can be found by doing:&lt;br /&gt;
&lt;br /&gt;
 [roberpj@gra-login2:~] module spider ls-dyna-mpi&lt;br /&gt;
        ls-dyna-mpi/7.1.2&lt;br /&gt;
        ls-dyna-mpi/7.1.3&lt;br /&gt;
        ls-dyna-mpi/8.1&lt;br /&gt;
        ls-dyna-mpi/9.1&lt;br /&gt;
        ls-dyna-mpi/9.2&lt;br /&gt;
        ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
To load the 7.1.X modules do the following:&lt;br /&gt;
 module load openmpi/1.6.5 ls-dyna-mpi/7.1.3&lt;br /&gt;
&lt;br /&gt;
To load the 8.X or 9.X modules do:&lt;br /&gt;
 module load openmpi/1.8.8 ls-dyna-mpi/9.2&lt;br /&gt;
&lt;br /&gt;
To load the 10.X modules do:&lt;br /&gt;
 module load openmpi/1.10.7 ls-dyna-mpi/10.0&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
===License===&lt;br /&gt;
&lt;br /&gt;
Once a license configuration is established, the research group will be given a 5 digit port number.  The value should then be inserted into the appropriate departmental export statement before loading the module file as follows:&lt;br /&gt;
&lt;br /&gt;
o UofT Mechanical Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o McGill Mechanical Engineering Department&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license2.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o UW Mechanical and Mechatronics Engineering Dept&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license3.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Laurentian University Bharti School of Engineering&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;Port&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license4.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
o Fictitious Example:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;purple&amp;quot;&amp;gt;export&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;LSTC_LICENSE_SERVER&amp;lt;/font&amp;gt;=&amp;lt;b&amp;gt;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;12345&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;@license1.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
===Module===&lt;br /&gt;
&lt;br /&gt;
The next step is load the desired sharcnet lsdyna module version.  Check which modules are available by running the &amp;lt;i&amp;gt;module avail&amp;lt;/i&amp;gt; command then load one the modules as shown:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] module avail lsdyna&lt;br /&gt;
------------------------------------ /opt/sharcnet/modules ------------------------------------&lt;br /&gt;
lsdyna/hyb/r711.88920     lsdyna/mpp/r711.88920     lsdyna/smp/r611.79036&lt;br /&gt;
lsdyna/mpp/ls971.r85718   lsdyna/mpp/r712.95028     lsdyna/smp/r711.88920&lt;br /&gt;
lsdyna/mpp/ls980B1.011113 lsdyna/mpp/r800.95359     lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna/mpp/r611.79036     lsdyna/mpp/r901.109912    lsdyna/smp/r800.95359&lt;br /&gt;
lsdyna/mpp/r611.80542     lsdyna/smp/ls980B1.78258  lsdyna/smp/r901.109912&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r9.0.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r901.109912&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r901.109912&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r8.0.0 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r800.95359&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna mkl&lt;br /&gt;
module load intel/15.0.3 openmpi/intel1503-std/1.8.7 lsdyna/mpp/r800.95359&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.2 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r712.95028&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r7.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/mpp/r711.88920&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====r6.1.1 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/r611.79036&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
or&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.80542&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;i&amp;gt;r611.80542&amp;lt;/i&amp;gt; provides lsdyna_s only.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====ls980 versions====&lt;br /&gt;
&lt;br /&gt;
o For serial or threaded jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;module load lsdyna/smp/ls980B1.78258&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
o For mpi jobs:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.4.5 lsdyna/mpp/ls980B1.011113&lt;br /&gt;
or&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/smp/ls980B1.78258&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note1) restart capability for ls980smpB1 and ls980mppB1 is not supported&lt;br /&gt;
Note2) the module using legacy openmpi/intel/1.4.5 will run extremely slow&lt;br /&gt;
&lt;br /&gt;
=Job Submission=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
The submission scripts myompjob.sh and mympijob.sh for the airbag problem are shown in the &amp;lt;b&amp;gt;Example Job&amp;lt;/b&amp;gt; section below, for both graham and orca.  Please note that you should specify your own username if submitting to a def account (not roberpj).  Alternatively you could specify your resource allocation account:&lt;br /&gt;
&lt;br /&gt;
Sample threaded job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch myompjob.sh&lt;br /&gt;
&lt;br /&gt;
Sample mpi job submit script:&lt;br /&gt;
&lt;br /&gt;
 sbatch  mympijob.sh&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
When loading a lsdyna sharcnet legacy module on the new orca or a sharcnet legacy system, the single or double precision solvers are specified with &amp;lt;strong&amp;gt;lsdyna_s&amp;lt;/strong&amp;gt; or &amp;lt;strong&amp;gt;lsdyna_d&amp;lt;/strong&amp;gt; respectively, as shown in the following sqsub commands:&lt;br /&gt;
&lt;br /&gt;
===1cpu SERIAL Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q serial -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===4cpu SMP Job===&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=1G lsdyna_s i=airbag.deploy.k ncpu=4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If using an explicit solver, one can specify a conservative initial memory setting on the command line as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q threaded -n 4 -o ofile.%J --mpp=2G lsdyna_d i=airbag.deploy.k ncpu=4 memory=754414&amp;lt;/pre&amp;gt;&lt;br /&gt;
where memory is the minimum number of 8 byte words shared by all processors in double precision, or, 4 byte words in single precision.&lt;br /&gt;
&lt;br /&gt;
The initial value can be determined by starting a simulation interactively on the command line, and finding the output resembling: &amp;lt;i&amp;gt; Memory required to begin solution      :      754414&amp;lt;/i&amp;gt;.    The number of words can be specified as memory=260M instead of memory=260000000,  for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  &lt;br /&gt;
&lt;br /&gt;
===8cpu MPI Job===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The initial memory can also be specified for mpi jobs  on the sqsub command line with &amp;quot;memory=&amp;quot; for the first master processor to decompose the problem  and &amp;quot;memory2=&amp;quot; used on all processors including the master to solves the decomposed problem, where the values are specified as 4 bytes per word in single precision and 8 bytes per word in double precision.   The number of words can be specified as memory=260M instead of memory=260000000 OR memory2=260M instead of memory2=260000000 for further details see https://www.d3view.com/2006/10/a-few-words-on-memory-settings-in-ls-dyna/.  The initial values can be found by running simulation interactively on a orca compute node and checking the output for a line such as: &amp;lt;i&amp;gt;Memory required to begin solution (memory=     464898  memory2=     158794 )&amp;lt;/i&amp;gt; which could then be implemented for a job run in the queue by doing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto&lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898 memory2=158794&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The specification of memory2 on sharcnet or compute canada clusters is not beneficial since the queue reserves the same memory per core for all nodes, such that the decomposition master node process cannot be allocated to have larger system memory.  Therefore its sufficient to specify a single memory parameter by doing:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
export LSTC_MEMORY=auto &lt;br /&gt;
sqsub -r 1h -q mpi -o ofile.%J -n 8 --mpp=2G lsdyna_d i=airbag.deploy.k memory=464898&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where the LSTC_MEMORY variable will only allow the memory to grow for explicit simulations. The following slurm examples demonstrate how to prescribe the memory parameters exactly.&lt;br /&gt;
&lt;br /&gt;
=Example Job=&lt;br /&gt;
&lt;br /&gt;
Copy the airbag example to your account:&lt;br /&gt;
&lt;br /&gt;
 rsync -av orca.computecanada.ca:/opt/sharcnet/lsdyna/r901.109912/examples r901.109912_examples&lt;br /&gt;
 cd r901.109912_examples/examples/misc/airbag&lt;br /&gt;
 gunzip airbag.deploy.k.gz&lt;br /&gt;
&lt;br /&gt;
==Graham (default modules)==&lt;br /&gt;
&lt;br /&gt;
Please note that graham does not have the sharcnet legacy modules installed on it.&lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mysmpjob1.sh&amp;lt;/b&amp;gt; for 4 core single precision smp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=4                   # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testsmp1                 # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load ls-dyna/9.1&lt;br /&gt;
ls-dyna_s i=airbag.deploy.k ncpu=4 memory=1000M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 4 (bytes/word) = 1000M words&lt;br /&gt;
&lt;br /&gt;
===Mpi Job ===&lt;br /&gt;
&lt;br /&gt;
Sample submission script &amp;lt;b&amp;gt;mympijob2.sh&amp;lt;/b&amp;gt; for 4 core double precision mpp job using compute canada default cvmfs modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --ntasks=4                     # number of ranks&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M            # memory per rank&lt;br /&gt;
#SBATCH --time=00:30:00                # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=testmpi2            # %x = jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server do either:&lt;br /&gt;
# 1) export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# 2) create  ~/.licenses/ls-dyna.lic as shown above&lt;br /&gt;
# ----------------------------------------------------------&lt;br /&gt;
module load openmpi/1.8.8 ls-dyna-mpi/9.1&lt;br /&gt;
srun ls-dyna_d i=airbag.deploy.k memory=500M memory2=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
==New Orca (legacy modules)==&lt;br /&gt;
&lt;br /&gt;
Please note, while new orca has most compute canada cvmfs modules available by default it does not have the graham the &amp;lt;i&amp;gt;ls-dyna&amp;lt;/i&amp;gt; or &amp;lt;i&amp;gt;ls-dyna-mpi&amp;lt;/i&amp;gt; modules installed.  Therefore currently the only way to run lsdyna is by using the legacy sharcnet &amp;lt;i&amp;gt;lsdyna&amp;lt;/i&amp;gt; modules as shown in the following two smp and mpi examples. &lt;br /&gt;
&lt;br /&gt;
===Threaded Job ===&lt;br /&gt;
&lt;br /&gt;
Submission script &amp;lt;b&amp;gt;mysmpjob3.sh&amp;lt;/b&amp;gt; to run 16 core single precision single node smp job with sharcnet legacy modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --nodes=1                           # use one node&lt;br /&gt;
#SBATCH --cpus-per-task=16                  # number threads&lt;br /&gt;
#SBATCH --mem=4000M                         # total memory&lt;br /&gt;
#SBATCH --time=00:30:00                     # hrs:min:sec&lt;br /&gt;
#SBATCH --job-name=mysmpjob3-1node-16core    # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
echo &amp;quot;SLURM_CPUS_PER_TASK= &amp;quot;$SLURM_CPUS_PER_TASK&lt;br /&gt;
export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
# IMPORTANT:  to set license server for legacy modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=31047@license3.sharcnet.ca&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does NOT work with legacy modules&lt;br /&gt;
# ----------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load lsdyna/smp/r712.95028&lt;br /&gt;
lsdyna_s i=airbag.deploy.k ncpu=$SLURM_CPUS_PER_TASK memory=500M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 4000M / 8 (bytes/word) = 500M words&lt;br /&gt;
&lt;br /&gt;
===MPI Job ===&lt;br /&gt;
&lt;br /&gt;
To run the sharcnet legacy &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; efficiently over several compute nodes with many cores requires using orca's xeon nodes as shown in &amp;lt;b&amp;gt; mympijob4.sh&amp;lt;/b&amp;gt; below.   To run lsdyna on orca's opteron nodes with &amp;lt;i&amp;gt;module lsdyna/mpp/r712.95028&amp;lt;/i&amp;gt; requires using only &amp;lt;b&amp;gt;one&amp;lt;/b&amp;gt; opteron node as shown in &amp;lt;b&amp;gt; mympijob5.sh&amp;lt;/b&amp;gt; below.  The other legacy sharcnet lsdyna mpp modules, as shown in the Availability Table  lsdyna/mpp/ls971.r85718, lsdyna/mpp/ls980B1.011113, lsdyna/mpp/r611.79036, lsdyna/mpp/r611.80542, lsdyna/mpp/r711.88920, lsdyna/mpp/r800.95359 and lsdyna/mpp/r901.109912 have yet to be tested - if you want to use one but cannot get it working please open a ticket.  Note that lines containing a space between &amp;quot;# and SBATCH&amp;quot; are comments.  To activate such line(s) required removal of the space ie) &amp;quot;#SBATCH&amp;quot;.  Its recommended to use &amp;lt;i&amp;gt;module ls-dyna-mpi&amp;lt;/i&amp;gt; on graham instead since the machine is far newer, faster and reliable. &lt;br /&gt;
&lt;br /&gt;
====Multi Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob4.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=xeon&lt;br /&gt;
#SBATCH --ntasks=48&lt;br /&gt;
#SBATCH --mem-per-cpu=4000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob4-Xnodes-48cores-xeon  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out             # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE --mca plm_slurm_args '--cpu_bind=none' lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Single Node Slurm Script====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~/samples/lsdyna/airbag] cat mympijob5.sh&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
#SBATCH --account=def-roberpj-ab&lt;br /&gt;
#SBATCH --constraint=opteron&lt;br /&gt;
#SBATCH --nodes=1&lt;br /&gt;
# SBATCH --ntasks=24&lt;br /&gt;
# SBATCH --mem=0&lt;br /&gt;
#SBATCH --ntasks=16&lt;br /&gt;
#SBATCH --mem-per-cpu=1000M&lt;br /&gt;
#SBATCH --time=00:30:00&lt;br /&gt;
#SBATCH --job-name=mympijob5-1node-16cores-opteron  # jobname&lt;br /&gt;
#SBATCH --output=slurm-%x-%N-%j.out         # %x = jobname&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
# To set license server when using legacy sharcnet modules:&lt;br /&gt;
export LSTC_LICENSE_SERVER=port@some.license.server&lt;br /&gt;
# since ~/.licenses/ls-dyna.lic does not work with legacy modules.&lt;br /&gt;
# -------------------------------------------------------------&lt;br /&gt;
module purge --force&lt;br /&gt;
export MODULEPATH=/opt/sharcnet/modules&lt;br /&gt;
module load intel/12.1.3 openmpi/intel/1.6.5 lsdyna/mpp/r712.95028&lt;br /&gt;
MACHINEFILE=`srun hostname -s | tr '\n' ':' | sed 's/:/,/g' | sed 's/.$//'`&lt;br /&gt;
mpirun --host $MACHINEFILE lsdyna_s i=airbag.deploy.k memory=250M memory2=250M&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where memory = 1000M / 4 (bytes/word) = 250M words&lt;br /&gt;
&lt;br /&gt;
==Legacy Clusters==&lt;br /&gt;
&lt;br /&gt;
STEP1) The following shows sqsub submission of the [http://www.dynaexamples.com/examples-manual/misc/airbag airbag] example to the mpi queue.  Its recommended to first edit  &amp;lt;i&amp;gt;airbag.deploy.k&amp;lt;/i&amp;gt;  and change &amp;lt;i&amp;gt;endtim&amp;lt;/i&amp;gt; to 3.000E-00 so the job runs long enough to perform the restart in steps 2 and 3 below:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cp -a /opt/sharcnet/lsdyna/r611.79036/examples /scratch/$USER/test-lsdyna&lt;br /&gt;
cd /scratch/$USER/test-lsdyna/misc/airbag&lt;br /&gt;
gunzip airbag.deploy.k.gz&lt;br /&gt;
cp airbag.deploy.k airbag.deploy.restart.k&lt;br /&gt;
nano deploy.restart.k (reduce plot file creation frequency)&lt;br /&gt;
   *DATABASE_BINARY_D3PLOT&lt;br /&gt;
   $       dt      lcdtdeploy.restart.k&lt;br /&gt;
   1.000E-01   &amp;lt;--- change from 5.000E-04 &lt;br /&gt;
export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
module unload intel openmpi lsdyna&lt;br /&gt;
module load intel/11.1.069 openmpi/intel/1.6.4 lsdyna/mpp/r611.79036&lt;br /&gt;
sqsub -r 10m -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP2) With the job still running, use the echo command as follows to create a file called &amp;quot;D3KIL&amp;quot; that will trigger generation of restart files at which point the file D3KIL itself will be erased.  Do this once a day if data loss is critical to you OR once or twice just before the  &amp;lt;i&amp;gt;sqsub -r&amp;lt;/i&amp;gt; time limit is reached.  Further information can be found here http://www.dynasupport.com/tutorial/ls-dyna-users-guide/sense-switch-control:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
echo &amp;quot;sw3&amp;quot; &amp;gt; D3KIL&lt;br /&gt;
sqkill job#&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP3) Before the job can be restarted, the following two lines must be added to the &amp;lt;i&amp;gt;airbag.deploy.restart.k&amp;lt;/i&amp;gt; file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
*CHANGE_CURVE_DEFINITION&lt;br /&gt;
         1&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
STEP4) Now resubmit the job as follows using &amp;quot;r=&amp;quot; to specify the restart file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sqsub -r 1h -q mpi --mpp=2G -n 8 -o ofile.%J lsdyna_d i=airbag.deploy.restart.k r=d3dump01&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=General Notes=&lt;br /&gt;
&lt;br /&gt;
==Graham Cluster==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Command Line Use==&lt;br /&gt;
&lt;br /&gt;
Parallel lsdyna jobs can be run interactively on the command line (outside the queue) for short run testing purposes as follows:&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node MPP (mpi) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load intel/12.1.3 openmpi/intel/1.6.2 lsdyna/mpp/r711.88920&lt;br /&gt;
 mpirun -np 8 lsdyna_d i=airbag.deploy.k&lt;br /&gt;
&lt;br /&gt;
o Orca Development Node SMP (threaded) Example&lt;br /&gt;
&lt;br /&gt;
 ssh orca.sharcnet.ca  &lt;br /&gt;
 ssh orc-dev1  (or dev2,3,4)&lt;br /&gt;
 module unload intel openmpi lsdyna&lt;br /&gt;
 module load lsdyna/smp/r711.88920&lt;br /&gt;
 lsdyna_d i=airbag.deploy.k ncpu=8&lt;br /&gt;
&lt;br /&gt;
==Memory Issues==&lt;br /&gt;
&lt;br /&gt;
A minumum of mpp=2G is recommended although the memory requirement of a job may suggest much less is required. For instance setting mpp=1G for the airbag test job above will result in the following error when running a job in the queue:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
An attempt to set processor affinity has failed - please check to&lt;br /&gt;
ensure that your system supports such functionality. If so, then&lt;br /&gt;
this is probably something that should be reported to the OMPI developers.&lt;br /&gt;
--------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another error message which can occur after a job runs for a while if mpp is chosen to small is: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Process 45 &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; Signal 11 : Segmentation Violation &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To get an idea of the amount of memory a job used run grep on the output file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep Memory&lt;br /&gt;
     |    Distributed Memory Parallel                  |&lt;br /&gt;
 Memory size from command line:      500000,       500000&lt;br /&gt;
 Memory for the head node&lt;br /&gt;
 Memory installed (MB)        :        32237&lt;br /&gt;
 Memory free (MB)             :        11259&lt;br /&gt;
 Memory required (MB)         :            0&lt;br /&gt;
 Memory required to process keyword     :       458120&lt;br /&gt;
 Memory required for decomposition      :       458120&lt;br /&gt;
 Memory required to begin solution (memory=      458120 memory2=      230721)&lt;br /&gt;
 Max. Memory reqd for implicit sol: max used               0&lt;br /&gt;
 Max. Memory reqd for implicit sol: incore                 0&lt;br /&gt;
 Max. Memory reqd for implicit sol: oocore                 0&lt;br /&gt;
 Memory required to complete solution (memory=      458120 memory2=      230721)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Version and Revision==&lt;br /&gt;
&lt;br /&gt;
Again run grep on the output file to extract the major and minor revision:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login1:~/samples/lsdyna/airbag]  cat  ofile.2063302.orc-admin2.orca.sharcnet | grep 'Revision\|Version'&lt;br /&gt;
     |  Version : mpp s R6.1.1    Date: 01/02/2013     |&lt;br /&gt;
     |  Revision: 78769           Time: 07:43:30       |&lt;br /&gt;
     |  SVN Version: 80542                             |&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Check License Status==&lt;br /&gt;
&lt;br /&gt;
===Running and Queued Programs===&lt;br /&gt;
&lt;br /&gt;
To get a summary of running and/or queued jobs use the &amp;lt;i&amp;gt;lstc_qrun&amp;lt;/i&amp;gt; command as follows, where queued means the job has started running according to the sqsub command but its actually sitting waiting for license resources to come available.  Once these are acquired the job will start running on the cluster and appear as a Running Program according to  lstc_qrun ie)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
                     Running Programs&lt;br /&gt;
    User             Host          Program              Started       # procs&lt;br /&gt;
-----------------------------------------------------------------------------&lt;br /&gt;
dgierczy    20277@saw61.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dgierczy     8570@saw32.saw.sharcn LS-DYNA_971      Mon Apr  8 17:44     1&lt;br /&gt;
dscronin    25486@hnd6             MPPDYNA_971      Tue Apr  9 20:19     6&lt;br /&gt;
dscronin    14897@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    14971@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    15046@hnd18            MPPDYNA_971      Tue Apr  9 21:48     6&lt;br /&gt;
dscronin    31237@hnd16            MPPDYNA_971      Tue Apr  9 21:53     6&lt;br /&gt;
dscronin    31313@hnd16            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
dscronin     6396@hnd15            MPPDYNA_971      Tue Apr  9 21:54     6&lt;br /&gt;
csharson    28890@saw175.saw.sharc MPPDYNA_971      Wed Apr 10 16:48     6&lt;br /&gt;
No programs queued&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To show license file expiration details, append &amp;quot;-R&amp;quot; to the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
[roberpj@hnd19:~] export LSTC_LICENSE_SERVER=#####@license#.uwo.sharcnet&lt;br /&gt;
&lt;br /&gt;
[roberpj@hnd19:~] lstc_qrun -R&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
**** LICENSE INFORMATION ****&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-DYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dgierczy   20277@saw61.saw.sharcnet   1&lt;br /&gt;
 dgierczy    8570@saw32.saw.sharcnet   1&lt;br /&gt;
MPPDYNA_971      12/31/2013          -    966   1024 |     0&lt;br /&gt;
 dscronin   25486@hnd6               6&lt;br /&gt;
 dscronin   14897@hnd18              6&lt;br /&gt;
 dscronin   14971@hnd18              6&lt;br /&gt;
 dscronin   15046@hnd18              6&lt;br /&gt;
 dscronin   31237@hnd16              6&lt;br /&gt;
 dscronin   31313@hnd16              6&lt;br /&gt;
 dscronin    6396@hnd15              6&lt;br /&gt;
 csharson   28890@saw175.saw.sharcnet   6&lt;br /&gt;
                   LICENSE GROUP    58    966   1024 |     0&lt;br /&gt;
&lt;br /&gt;
PROGRAM          EXPIRATION CPUS  USED   FREE    MAX | QUEUE&lt;br /&gt;
---------------- ----------      ----- ------ ------ | -----&lt;br /&gt;
LS-OPT           12/31/2013          0   1024   1024 |     0&lt;br /&gt;
                   LICENSE GROUP     0   1024   1024 |     0&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Killling A Program===&lt;br /&gt;
&lt;br /&gt;
The queue normally kills lsdyna jobs cleanly.  However its possible that licences for a job (which is no longer running in the queue and therefore no longer has any processes running on the cluster) will continue to be tied up according to the lstc_qrun command.   To kill such a program determine the pid@hostname from the lstc_qrun command then run the following kill command.  The following example demonstrates the procedure for username roberpj:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
roberpj    45312@orc329.orca.shar MPPDYNA          Wed Dec  5 15:50    12&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~] lstc_qkill 45312@orc329.orca.sharcnet.ca&lt;br /&gt;
Defaulting to server 1 specified by LSTC_LICENSE_SERVER variable&lt;br /&gt;
Program queued for termination&lt;br /&gt;
&lt;br /&gt;
[roberpj@orc-login2:~]  lstc_qrun -R | grep roberpj&lt;br /&gt;
[roberpj@orc-login2:~] &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Legacy Instructions==&lt;br /&gt;
&lt;br /&gt;
The following binaries remain available on saw and orca for backward compatibility testing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[roberpj@orc-login2:~] cd /opt/sharcnet/local/lsdyna&lt;br /&gt;
[roberpj@orc129:/opt/sharcnet/local/lsdyna] ls ls971*&lt;br /&gt;
ls971_d_R3_1    ls971_d_R4_2_1  ls971_s_R3_1    ls971_s_R4_2_1  ls971_s_R5_1_1&lt;br /&gt;
ls971_d_R4_2_0  ls971_d_R5_0    ls971_s_R4_2_0  ls971_s_R5_0&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are currently no sharcnet modules for these versions, hence jobs should be submitted as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module load lsdyna&lt;br /&gt;
export PATH=/opt/sharcnet/local/lsdyna:$PATH&lt;br /&gt;
export LSTC_LICENSE_SERVER=XXXXX@license3.uwo.sharcnet&lt;br /&gt;
cp /opt/sharcnet/local/lsdyna/examples/airbag.deploy.k airbag.deploy.k&lt;br /&gt;
SERIAL JOB:  sqsub -q serial -r 1d -o ofile.%J ls971_d_R4_2_1 i=airbag.deploy.k&lt;br /&gt;
THREADED JOB:  sqsub -q threaded -n 4 -r 1d -o ofile.%J ls971_d_R4_2_1 ncpu=4 para=2 i=airbag.deploy.k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note! the &amp;lt;i&amp;gt;ls971_s_R3_1&amp;lt;/i&amp;gt; and &amp;lt;i&amp;gt;ls971_d_R3_1&amp;lt;/i&amp;gt; binaries do not work, a fix is being looked for.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Homepage&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/products/ls-dyna&lt;br /&gt;
&lt;br /&gt;
o LSTC LS-DYNA Support (Tutorials, HowTos, Faq, Manuals, Release Notes, News, Links)&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Release Notes&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/release-notes&lt;br /&gt;
&lt;br /&gt;
o LS-PrePost Online Documentation FAQ&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/lspp/content/faq.shtml&lt;br /&gt;
&lt;br /&gt;
o LSTC Download/Install Overview Page&amp;lt;br&amp;gt;&lt;br /&gt;
Provides links to binaries for LS-DYNA SMP/MPP, LS-OPT, LS-PREPOST, LS-TASC.&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.lstc.com/download&lt;br /&gt;
&lt;br /&gt;
==Memory Links==&lt;br /&gt;
&lt;br /&gt;
o Implicit: Memory notes&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.dynasupport.com/howtos/implicit/implicit-memory-notes&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA Support Environment variables&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.dynasupport.com/howtos/general/environment-variables&lt;br /&gt;
&lt;br /&gt;
o LS-DYNA and d3VIEW Blog (a few words on memory settings)&amp;lt;br&amp;gt;&lt;br /&gt;
http://blog2.d3view.com/a-few-words-on-memory-settings-in-ls-dyna/&lt;br /&gt;
&lt;br /&gt;
o Convert Words to GB ie) memory=500MW (3.73GB)&amp;lt;br&amp;gt;&lt;br /&gt;
http://deviceanalytics.com/memcalc.php&lt;/div&gt;</summary>
		<author><name>Roberpj</name></author>	</entry>

	</feed>