Skip to content

Commit 5541db6

Browse files
authored
Merge pull request #295 from NBISweden/pixi
Add extra material on Pixi
2 parents ffa37f7 + 1bb2feb commit 5541db6

File tree

2 files changed

+179
-1
lines changed

2 files changed

+179
-1
lines changed

home_precourse.qmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ conda config --env --set subdir osx-64
306306
```
307307
:::
308308

309-
310309
## Installing Snakemake
311310

312311
We will use Conda environments for the set up of this tutorial, but don't worry

pages/conda.qmd

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,182 @@ execution time between Conda and Mamba. Another reason is that Mamba colours its
636636
output, which is nice if you care about that sort of thing. If you installed
637637
Conda as described in the pre-course material you'll, conveniently, already have
638638
installed Mamba as well!
639+
640+
### Pixi, for a faster, project-centric approach
641+
642+
Pixi is another package management tool that builds on the Conda ecosystem but
643+
which has a more organised, project-centric approach. Pixi does not have a
644+
`base` environment either, which is a very nice feature, as you can completely
645+
focus on the environments that your projects needs. The project-centric
646+
approach also makes it easy to have multiple environments together. Importantly,
647+
Pixi is also significantly faster than both Conda and Mamba. Indeed, several of
648+
the teachers here at the course have moved away from using Conda/Mamba and now
649+
almost exclusively use Pixi instead.
650+
651+
::: {.callout-tip}
652+
If Pixi is so good, why is it in the "extra materials" section rather than as a
653+
replacement for Conda? Firstly, Conda is still widely adopted throughout the field,
654+
not only as a stand-alone tool, but it's also widely integrated into _other_
655+
tools such as IDEs, workflow managers, _etc._. Secondly, Pixi still uses the
656+
Conda ecosystem with channels, packages, _etc._, so learning Conda is still time
657+
well spent, as a lot of it transfers directly to Pixi.
658+
:::
659+
660+
Installing Pixi is simple. Just run:
661+
662+
```bash
663+
curl -fsSL https://pixi.sh/install.sh | bash
664+
```
665+
666+
Then restart your shell and make sure that Pixi is installed by running `pixi
667+
--version`.
668+
669+
Each Pixi _project_ is tied to a specific folder that contains a `pixi.toml`
670+
manifest file describing the project; the TOML format is similar to the YAML
671+
format we've already used in Conda, but with some structural differences. An
672+
example of a minimal `pixi.toml` file could look like this:
673+
674+
```{.toml filename="pixi.toml"}
675+
[project]
676+
name = "project_mrsa"
677+
channels = ["conda-forge", "bioconda"]
678+
platforms = ["linux-64", "osx-64"]
679+
````
680+
681+
::: {.callout-tip}
682+
If you are using VSCode you can install an extension to get syntax highlighting
683+
for TOML files. Search for "TOML Support" in the extensions marketplace.
684+
:::
685+
686+
The `pixi.toml` file is built up of several 'tables' which are collections of
687+
key/value pairs. In the example above we have the `[project]` table which
688+
defines general information about the project, for example the project name, the
689+
Conda channels to use and which platforms the project should be compatible with.
690+
These are the minimum requirements for a `pixi.toml` file.
691+
692+
But you can also add a lot more information to this table, such as `authors`,
693+
`description`, `version`, `license`, `homepage` _etc._
694+
695+
The easiest way to start a new project with Pixi is to run `pixi init` in the
696+
project folder. This will create a `pixi.toml` file with some default values
697+
that you can then edit to fit your project. Try this out in the
698+
`tutorials/conda/` folder!
699+
700+
Make sure you are in the `tutorials/conda/` folder and run:
701+
702+
```bash
703+
pixi init
704+
```
705+
706+
This will create a `pixi.toml` file in the folder. Open it up and take a look.
707+
708+
You should see something similar to the example above, but likely Pixi also
709+
added the `authors` key (where the value is the name and email address taken
710+
from your global git configuration), a `description` and a `version`. You can
711+
edit these values as you see fit.
712+
713+
Let's add the `bioconda` channel to the `channels` list. Edit the `channels`
714+
key/value pair in the `pixi.toml` file so it looks like this:
715+
716+
```{.toml filename="pixi.toml"}
717+
channels = ["conda-forge", "bioconda"]
718+
```
719+
720+
At the end of the file there should also be two empty placeholder tables:
721+
`[tasks]` and `[dependencies]`. The tasks table allows you to add custom shell
722+
commands to your project. You can leave the tasks table empty as we won't go
723+
through it in this tutorial, but you can read more about this in the [Pixi
724+
documentation](https://pixi.sh/latest/reference/pixi_manifest/#the-tasks-table).
725+
726+
Let's instead focus on the `[dependencies]` table. This is where you specify the
727+
software dependencies for your project, similar to how you add package names and
728+
versions in a Conda environment file. These dependencies can be specified in
729+
several ways, the most simple being _e.g._:
730+
731+
```{.toml}
732+
[dependencies]
733+
python = "*"
734+
```
735+
736+
This will install the most recent version (the `*` character is a wildcard which
737+
translates into 'any version') of Python available in the channels specified in
738+
the `channels` key. You can also specify an explicit version number, _e.g._
739+
`python = "3.8"` or combine this with an operator, _e.g._ `python = ">=3.8"` to
740+
specify that you want Python version 3.8 or later.
741+
742+
Let's add the `fastqc` package to the `[dependencies]` table. Edit this section
743+
in the `pixi.toml` file so it looks like this:
744+
745+
```{.toml filename="pixi.toml"}
746+
[dependencies]
747+
fastqc = "*"
748+
```
749+
750+
Now let's use the `pixi.toml` file to create the environment for the project. Run:
751+
752+
```bash
753+
pixi install
754+
```
755+
756+
Pixi will download and install the packages specified under the `[dependencies]`
757+
table (for now just `fastqc`) and then report:
758+
759+
```bash
760+
✔ The default environment has been installed.
761+
```
762+
763+
You should now see a new file `pixi.lock` inside your current folder. This file
764+
was generated by Pixi during the `install` step and contains information about
765+
the environment that was created and the packages it contains. Importantly, the
766+
lock file should be treated as read-only, **never edit this file by hand**.
767+
768+
If you list hidden files and folder inside the directory (`ls -l`) you will also
769+
see a `.pixi/` folder. This, or more specifically the `.pixi/envs/default/`
770+
folder, is where the environment is stored by default. You can define several
771+
[environments](https://pixi.sh/latest/reference/pixi_manifest/#the-feature-and-environments-tables)
772+
in the `pixi.toml` file and each environment will get a sub folder in
773+
`.pixi/envs/`. Here we only have one environment, the `default` environment.
774+
775+
So how do you activate the environment? You can do this by running:
776+
777+
```bash
778+
pixi shell
779+
```
780+
781+
This will start a new shell in the project environment and as you can see the
782+
name of the project is now prepended to the prompt. Try to run `fastqc
783+
--version` to see that the software is installed and working. To exit the
784+
shell/environment simply type `exit` (or hit `Ctrl+D`).
785+
786+
Let's add a few more packages to the `[dependencies]` table. Edit the
787+
`pixi.toml` file so that the `[dependencies]` table looks like this:
788+
789+
```{.toml filename="pixi.toml"}
790+
[dependencies]
791+
fastqc = "*"
792+
multiqc = "*"
793+
seqtk = "*"
794+
snakemake = ">=8"
795+
bowtie2 = "*"
796+
samtools = "*"
797+
subread = "*"
798+
```
799+
800+
Now directly run `pixi shell` from the command line. Because you updated the
801+
`pixi.toml` file Pixi will automatically detect that the environment needs to be
802+
updated and will do so before starting the shell. Consequently, the `pixi.lock`
803+
file will be updated to reflect the new environment. Try running _e.g._
804+
`snakemake --version` to see that the new software is installed and working.
805+
806+
::: {.callout-tip}
807+
To see which packages are installed in the environment you can run `pixi list`
808+
from inside the project directory.
809+
:::
810+
811+
This was a very short introduction to Pixi, focusing on how it can be used to
812+
achieve the same type of functionality as Conda. As you can see, in Pixi the
813+
emphasis is on projects rather than environments. Whether this makes your work
814+
more organised and easier to manage is for you to decide.
815+
816+
There is much more you can do with this tool though so if you want to learn more
817+
about it you can read the [Pixi documentation](https://pixi.sh/latest/).

0 commit comments

Comments
 (0)