Skip to content

Commit e8f55b3

Browse files
committed
added: new block about nextjs & nix
1 parent 35ad8e3 commit e8f55b3

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/Nix-OS/package-nextjs-Nix.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
## Packaging Next-JS Webapps on Nix
2+
3+
Recently, I just started to learn [NextJS](https://nextjs.org) to broaden my security knowledge, since my primer OS is [Nix](https://nixos.org), there is a need of configuring NextJS environment. This Post will explain how I package a NextJS [Webapp](https://en.wikipedia.org/wiki/Web_application) in my [Nix-OS](https://github.com/codedsprit/nix).
4+
5+
### Packaging
6+
#### Statically Exported Webapps
7+
Statically exported ones are easy to package, because it is a matter of running npm build (or whatever your build script is) with the following NextJS settings
8+
```js
9+
// next.config.js
10+
module.exports = {
11+
distDir: "dist", // an artitrary path for your export
12+
output: "export",
13+
};
14+
```
15+
This will export a static website with a bunch of html files that you can then serve with nodePackages.serve or a webserver like nginx or apache. And that is the end of your worries for a statically exported website! No headache, just write a simple derivation, such as the one below
16+
```bash
17+
# default.nix
18+
{
19+
buildNpmPackage,
20+
pkg-config,
21+
python3,
22+
...
23+
}:
24+
buildNpmPackage {
25+
pname = "your-website";
26+
version = "0.1";
27+
28+
src = ./.;
29+
# needs to be updated everytime you update npm dependencies
30+
npmDepsHash = "sha256-some-hash";
31+
# some npm packages may need to be built from source, because nodejs is a *terrible* ecosystem
32+
nativeBuildInputs = [pkg-config python3];
33+
34+
# move exported website to $out
35+
postInstall = ''
36+
cp -rf dist/* $out
37+
'';
38+
}
39+
```
40+
### Webapps that cannot be statically exported
41+
If your website depends on API routes for some reasons, then Next will not allow you to do static export. Which means you need to run next start in some shape or form. While a systemd service is certainly a way of doing it (one that I do not recommend), a oci container works as well if not better.
42+
43+
You can write a "simple" docker image for your oci container to use, such as the one below
44+
45+
```bash
46+
# dockerImage.nix
47+
{
48+
pkgs,
49+
inputs,
50+
...
51+
}: {
52+
dockerImage = pkgs.dockerTools.buildImage {
53+
config = {
54+
WorkingDir = "/your-website";
55+
Cmd = ["npm" "run" "serve"];
56+
};
57+
58+
name = "your-website";
59+
tag = "latest";
60+
61+
fromImage = pkgs.dockerTools.buildImage {
62+
name = "node";
63+
tag = "18-alpine";
64+
};
65+
66+
copyToRoot = pkgs.buildEnv {
67+
name = "image-root";
68+
69+
paths = with pkgs; [
70+
# this package is called from a flake.nix alongside the derivation for the website
71+
inputs.self.packages.${pkgs.stdenv.system}.your-website
72+
nodejs
73+
bash
74+
];
75+
76+
pathsToLink = [
77+
"/bin"
78+
"/your-website"
79+
];
80+
};
81+
};
82+
}
83+
84+
```
85+
Then, configure oci-containers module option to pick up the Docker image that you have built.
86+
87+
```bash
88+
virtualisation.oci-containers = {
89+
backend = "podman";
90+
containers = {
91+
"website-container" = {
92+
autoStart = true;
93+
ports = [
94+
"3000:3000" # bind container's port 3000 to the outside port 3000 for NextJS
95+
];
96+
97+
extraOptions = ["--network=host"];
98+
99+
image = "your-website";
100+
imageFile = inputs.website-flake.packages.${pkgs.stdenv.system}.dockerImage;
101+
};
102+
};
103+
};
104+
```
105+
After a rebuild, your system will provision the container and start it on port 3000. You can access it with your-server-ip:3000 in your browser, and even configure nginx to set up a reverse proxy to assign your domain.
106+
```bash
107+
"example.com" = {
108+
locations."/".proxyPass = "http://127.0.0.1:3000";
109+
};
110+
```
111+
This will assign your domain to your webserver, and allow outside visitors to view your "awesome" NextJS webapp.

src/Nix-OS/package_nexjs_on_Nix.md

Whitespace-only changes.

src/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
[About](./about.md)
44

5+
6+
# Nix-OS
7+
- [package nextjs in nix](./Nix-OS/package-nextjs-Nix.md)
8+

0 commit comments

Comments
 (0)