In this tutorial, you will learn how to add flake.nix
to a TypeScript web application using node-typescript
template in the repository.
Initialize a new project
You will need one of
npm
,yarn
,pnpm
, orbun
. Bothnpm
andbun
are available directly from Nixpkgs.If you prefer
yarn
orpnpm
, you will probably need bothyarn
/pnpm
andnpm
. To make the multiple packages available, you can use the classicnix-shell
command, or use a devshell in my personal repository.Terminal window nix shell nixpkgs#nodejsTerminal window nix-shell -p yarn -p nodejsor
Terminal window nix develop github:akirak/flake-pins#yarnTerminal window nix-shell -p pnpm -p nodejsor
Terminal window nix develop github:akirak/flake-pins#pnpmTerminal window nix shell nixpkgs#bunThen follow an instruction for the framework of your choice to initialize the project. Below are some links:
Add flake.nix to the project
Initialize the template from the root directory of the project:
Terminal window nix flake init github:akirak/flake-templates#node-typescriptIf the
package.json
in the repository containspackageManager
section, corepack installs the program automatically.Here is an example
packageManager
specification inpackage.json
:"packageManager": "pnpm@10.4.0+sha512.6b849d0787d97f8f4e1f03a9b8ff8f038e79e153d6f11ae539ae7c435ff9e796df6a862c991502695c7f9e8fac8aeafc1ac5a8dab47e36148d183832d886dd52",The
flake.nix
should containcorepack
package in the development shell:{devShells = eachSystem (pkgs: {default = pkgs.mkShell {buildInputs = [...# Use corepack to install npm/pnpm/yarn as specified in package.jsonpkgs.corepack...];};});};Alternatively, you can install
yarn
,pnpm
, orbun
directly using Nix. Openflake.nix
and comment out the corresponding package name in thepackage
field of the default shell, depending on the package manager of your choice.devShells = eachSystem (pkgs: {default = pkgs.mkShell {buildInputs = [pkgs.nodejs# Comment out one of these# pkgs.bun# pkgs.nodePackages.pnpm# pkgs.yarnpkgs.yarn...];};});There are several other options available. See the comments in the generated
flake.nix
.Add
.envrc
:Terminal window use flakeAllow direnv:
Terminal window direnv allow
Developing
You can use npm
, yarn
, pnpm
, or bun
to develop your application as you would normally do.