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
Section titled “Initialize a new project”You will need one of npm, yarn, pnpm, or bun. Both npm and bun are available directly from Nixpkgs.
If you prefer yarn or pnpm, you will probably need both yarn/pnpm and npm. To make the multiple packages available, you can use the classic nix-shell command, or use a devshell in my personal repository.
nix shell nixpkgs#nodejsnix-shell -p yarn -p nodejsnix-shell -p pnpm -p nodejsnix shell nixpkgs#bunFollow an instruction for the framework of your choice to initialize the project. Below are some links:
- Install and set up Astro | Docs
- Getting Started: Installation | Next.js
- Installation · Get Started with Nuxt
- Creating a project • Docs • Svelte
Add flake.nix to the project
Section titled “Add flake.nix to the project”Initialize the template from the root directory of the project:
Terminal window nix flake init -t github:akirak/flake-templates#node-typescriptIf the
package.jsonin the repository containspackageManagersection, corepack installs the program automatically.Here is an example
packageManagerspecification inpackage.json:"packageManager": "pnpm@10.4.0+sha512.6b849d0787d97f8f4e1f03a9b8ff8f038e79e153d6f11ae539ae7c435ff9e796df6a862c991502695c7f9e8fac8aeafc1ac5a8dab47e36148d183832d886dd52",The
flake.nixshould containcorepackpackage 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, orbundirectly using Nix. Openflake.nixand comment out the corresponding package name in thepackagefield 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
Section titled “Developing”You can use npm, yarn, pnpm, or bun to develop your application as you would normally do.