This commit is contained in:
Amy de Buitléir 2025-10-11 16:32:16 +01:00
parent 134d8da43c
commit 2945a70e3b
6 changed files with 184 additions and 101498 deletions

5813
index.html

File diff suppressed because it is too large Load diff

View file

@ -16,34 +16,18 @@ writing your own flakes. I'll explain the example in some detail.
inputs = { inputs = {
[.highlight01]#nixpkgs#.url = "github:NixOS/nixpkgs"; [.highlight01]#nixpkgs#.url = "github:NixOS/nixpkgs";
[.highlight02]#flake-utils#.url = "github:numtide/flake-utils";
[.highlight03]#..._other dependencies_...# ❶ [.highlight03]#..._other dependencies_...# ❶
}; };
outputs = { self, [.highlight01]#nixpkgs#, [.highlight02]#flake-utils#, [.highlight03]#..._other dependencies_...# ❷ }: outputs = { self, [.highlight01]#nixpkgs#, [.highlight03]#..._other dependencies_...# ❷ }: {
flake-utils.lib.eachDefaultSystem (system: ❸
let
pkgs = import nixpkgs { inherit system; };
python = pkgs.python3;
in
{
devShells = rec {
default = pkgs.mkShell {
packages = [ #_packages needed for development shell_#; ❹ ];
};
};
packages = rec { devShells = #_shell definitions_#; ❸
[.highlight04]#myPackageName# = #_package definition_#; ❺
default = [.highlight04]#myPackageName#;
};
apps = rec { packages = #_package definitions_#; ❹
[.highlight04]#myPackageName# = flake-utils.lib.mkApp { drv = self.packages.${system}.[.highlight04]#myPackageName#; };
default = [.highlight04]#myPackageName#; ❻ apps = #_app definitions_#; ❺
};
} };
);
} }
---- ----
@ -52,54 +36,84 @@ this part of the flake should be familiar. Remember also that any
dependencies in the input section should also be listed at the beginning dependencies in the input section should also be listed at the beginning
of the outputs section `❷`. of the outputs section `❷`.
Now it's time to look at the content of the output section. If we want The `devShells` attribute `❸` specifies the environment that should be
the package to be available for multiple systems (e.g., available when doing development on the package.
"`x86_64-linux`", "`aarch64-linux`", "`x86_64-darwin`", and This includes any tools
"`aarch64-darwin`"), we need to define the output for each of those (e.g., compilers and other language-specific build tools and packages).
systems. Often the definitions are identical, apart from the name of the If you don't need a special development environment, you can omit this section.
system. The eachDefaultSystem function `❸` provided by flake-utils allows
us to write a single definition using a variable for the system name.
The function then iterates over all default systems to generate the
outputs for each one.
The `devShells` variable specifies the environment that should be The `packages` attribute `❹` defines the packages that this flake provides.
available when doing development on the package. If you don't need a The definition depends on the programming languages your
special development environment, you can omit this section. At `❹` you software is written in, the build system you use, and more.
would list any tools (e.g., compilers and language-specific build tools) There are Nix functions and tools that can simplify much of this, and new,
you want to have available in a development shell. If the compiler needs easier-to-use ones are released regularly.
access to language-specific packages, there are Nix functions to assist These functions are very language-specific, and not always well-documented.
with that. These functions are very language-specific, and not always We will see examples for some languages later in the tutorial.
well-documented. We will see examples for some languages later in the In general, I recommend that you do a web search for
tutorial. In general, I recommend that you do a web search for
"nix _language-name_", and try to find resources that were written or updated "nix _language-name_", and try to find resources that were written or updated
recently. recently.
The `packages` variable defines the packages that this flake provides. The `apps` attribute `❺` identifies any applications provided by the flake.
The package definition `❺` depends on the programming languages your In particular, it identifies the default executable that `nix run`
software is written in, the build system you use, and more. There are
Nix functions and tools that can simplify much of this, and new,
easier-to-use ones are released regularly. Again, I recommend that you
do a web search for "nix _language-name_", and try to find resources that
were written or updated recently.
The `apps` variable identifies any applications provided by the flake.
In particular, it identifies the default executable ❻ that `nix run`
will run if you don't specify an app. will run if you don't specify an app.
If we want the development shell, packages, and apps
to be available for multiple systems
(e.g., `x86_64-linux`, `aarch64-linux`, `x86_64-darwin`, and
`aarch64-darwin`),
we need to provide a definition for each of those systems.
The result _could_ be an outputs section like the one shown below.
[source,subs=quotes]
----
outputs = { self, [.highlight01]#nixpkgs#, [.highlight03]#..._other dependencies_...# ❷ }: {
devShells.x86_64-linux.default = ...;
devShells.aarch64-linux.default = ...;
. . .
packages.x86_64-linux.my-app = ...;
packages.aarch64-linux.my-app = ...;
. . .
apps.x86_64-linux.my-app = ...;
apps.aarch64-linux.my-app = ...;
. . .
apps.x86_64-linux.default = ...;
apps.aarch64-linux.default = ...;
. . .
};
----
You won't see definitions like that in most flakes.
Typically the definitions for each shell, package or app
would be identical, apart from a reference to the system name.
There are techniques and tools that allow you to write a single definition
and use it to automatically generate the definitions for multiple architectures.
We will see some examples of this later in the tutorial.
Below is a list of some functions that are commonly used in Below is a list of some functions that are commonly used in
this section. the output section.
General-purpose:: General-purpose::
The standard environment provides - https://noogle.dev/f/pkgs/stdenv/mkDerivation[`mkDerivation`]
https://noogle.dev/f/pkgs/stdenv/mkDerivation[`mkDerivation`], is especially useful for the typical
which is especially useful for the typical
`./configure; make; make install` scenario. `./configure; make; make install` scenario.
It's customisable. It's customisable.
- https://nixos.org/manual/nixpkgs/unstable/#sec-pkgs-mkShell[`mkShell`]
simplifies writing a development shell definition.
- https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeShellApplication[`writeShellApplication`]
creates an executable shell script which also defines the appropriate environment.
Python:: Python::
`buildPythonApplication`, `buildPythonPackage`. - `buildPythonApplication`
- `buildPythonPackage`.
Haskell:: Haskell::
`mkDerivation` (Haskell version, which is a wrapper around the - `mkDerivation` (Haskell version, which is a wrapper around the
standard environment version), `developPackage`, `callCabal2Nix`. standard environment version)
- `developPackage`
- `callCabal2Nix`.
[NOTE] [NOTE]

View file

@ -109,3 +109,11 @@ Remove one or more keys and associated values from a set.
nix-repl> builtins.removeAttrs animal [ "age" "species" ] nix-repl> builtins.removeAttrs animal [ "age" "species" ]
{ name = "Professor Paws"; } { name = "Professor Paws"; }
.... ....
Display an attribute set, including nested sets.
[source]
....
nix-repl> builtins.toJSON animal
"{\"age\":10,\"name\":{\"first\":\"Professor\",\"last\":\"Paws\"},\"species\":\"cat\"}"
....

View file

@ -0,0 +1,77 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"hello-nix": {
"flake": false,
"locked": {
"lastModified": 1757705465,
"narHash": "sha256-sJCQ9+8Dy+QF9ISaupp42+mGbuXtFyqbX85tWzeNPOI=",
"ref": "refs/heads/main",
"rev": "56044f61231c996e4ab795de1da89e5f79db3f4f",
"revCount": 5,
"type": "git",
"url": "https://codeberg.org/mhwombat/hello-nix"
},
"original": {
"type": "git",
"url": "https://codeberg.org/mhwombat/hello-nix"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1760194782,
"narHash": "sha256-fJqK9vyL6Ur2HbtJX1xtBN499+18XLeshXPb/B+Udgw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f84bf9b679e4b8524ab2a90cf854a4f691739457",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"hello-nix": "hello-nix",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View file

@ -0,0 +1,27 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
hello-nix = {
url = "git+https://codeberg.org/mhwombat/hello-nix";
flake = false;
};
};
outputs = { self, nixpkgs, flake-utils, hello-nix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
helloNix = import hello-nix { inherit pkgs; };
in
{
devShells = rec {
default = pkgs.mkShell {
packages = [ helloNix ];
};
};
}
);
}

File diff suppressed because it is too large Load diff