Skip to content

Commit

Permalink
Update 11-nix.qmd
Browse files Browse the repository at this point in the history
  • Loading branch information
b-rodrigues authored Oct 31, 2023
1 parent 32e3587 commit 1db0bfe
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions 11-nix.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ that Java is required and installs and configures it. It all just happens withou
of Nix is that it is possible to pin a certain revision of the Nix packages’ repository (called nixpkgs) for our project. Pinning a revision
ensures that every package that Nix installs will always be at exactly the same versions, regardless of when in the future the packages get installed.

With Nix, it is essentially possible to replace {renv} and Docker combined. If you need other tools or languages like Python or Julia,
this can also be done easily. Nix is available for Linux, macOS and Windows (via WSL2). Important remark: since using Nix on Windows
must go through WSL, when we refer to "Linux" in the context of Nix, this includes Windows by default as well.
With Nix, it is essentially possible to replace {renv} and Docker combined, or if you’re using mainly Python, you can replace `conda` or `requirements.txt`
files. If you need other tools or languages like Python or Julia, this can also be done easily. Nix is available for Linux, macOS and Windows (via WSL2).
Important remark: since using Nix on Windows must go through WSL, when we refer to "Linux" in the context of Nix, this includes Windows by default as well.
It is also possible to build multi-language environments, containing R and Python, a LaTeX distribution and packages and so on.

## The Nix programming language

Expand All @@ -53,8 +54,53 @@ For example,
is the Nix expression that contains the derivation to build `quarto`. As you can see, the derivation uses the the pre-built Quarto binaries
instead of building it from source. Adding packages to nixpkgs (or updating them) can be done by opening pull requests. For example,
[here](https://github.com/NixOS/nixpkgs/pull/259443)^[https://github.com/NixOS/nixpkgs/pull/259443] is a pull request to make Quarto
available to all platforms (before this PR Quarto was only available for Linux).
available to all platforms (before this PR Quarto was only available for Linux). PRs get reviewed and approuved by maintainers that also
have the right to merge the PR into master. Once merged, the new or updated package is available for download.
Because nixpkgs is a “just” Github repository, it is possible to use a specific commit hash to install the packages as they were at
a specific point in time. For example, if you use this commit, 7c9cc5a6e, you’ll get the packages as of the 19th of October 2023,
but if you used this one instead: 976fa3369, you’ll get packages from the 19th of August 2023. Using specific hashes is called
"pinning" and you can read more about it [here](https://nixos.wiki/wiki/FAQ/Pinning_Nixpkgs). We will make extensive use of pinning.

## The NixOS operating system
## The NixOS operating system, Docker and Github Actions

NixOS is a Linux distribution that uses the Nix package manager as its package manager. I won’t go into detail here, but you should know it
exists. What’s perhaps more interesting for our purposes is to use Nix within Docker. Because Nix can be installed as any other tool, you could
very well build a Docker image that starts by installing Nix, and then uses Nix to install, in a reproducible manner, all the tools you need
for your project.

There are also a series of Github Actions that you can use to install Nix on runners and build development environments. We will also look that.

## A first Nix expression

The following expression is the one that defines the development environment to build this book:

```
let
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/976fa3369d722e76f37c77493d99829540d43845.tar.gz") {};
rpkgs = builtins.attrValues {
inherit (pkgs.rPackages) quarto Ecdat devtools janitor plm pwt9 rio targets tarchetypes testthat tidyverse usethis formatR;
};
tex = (pkgs.texlive.combine {
inherit (pkgs.texlive) scheme-small amsmath framed fvextra environ fontawesome5 orcidlink pdfcol tcolorbox tikzfill;
});
system_packages = builtins.attrValues {
inherit (pkgs) R glibcLocalesUtf8 quarto;
};
in
pkgs.mkShell {
LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then "${pkgs.glibcLocalesUtf8}/lib/locale/locale-archive" else "";
LANG = "en_US.UTF-8";
LC_ALL = "en_US.UTF-8";
LC_TIME = "en_US.UTF-8";
LC_MONETARY = "en_US.UTF-8";
LC_PAPER = "en_US.UTF-8";
LC_MEASUREMENT = "en_US.UTF-8";
buildInputs = [ rpkgs tex system_packages ];
}
```

I won’t go into much detail, but here are some pointers:

## The {rix} package

0 comments on commit 1db0bfe

Please sign in to comment.