Skip to content

Commit 69b8034

Browse files
authored
Initial commit
0 parents  commit 69b8034

File tree

13 files changed

+343
-0
lines changed

13 files changed

+343
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
result

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Openmesh Network
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Xnode Node.js Template
2+
3+
Template to make your Node.js app Xnode/Nix compatible.
4+
5+
## Modification Steps
6+
7+
1. Replace all instances of "xnode-nodejs-template" with the name of your project. This should be unique, as no apps with the same name can be run on a single Xnode.
8+
2. Build your Node.js app
9+
3. Once your app is ready for deployment and runs using `nix run`, push to GitHub and copy your GitHub url (e.g. https://github.com/Openmesh-Network/xnode-nodejs-template).
10+
4. Go the the App Store in [Xnode Studio](https://www.openmesh.network/xnode/app-store) and go to the advanced tab.
11+
5. Paste your GitHub url and enter the name of the project you choose during step 1.
12+
6. Hit deploy and wait for your app to be ready.
13+
7. Copy the deploy link and replace the one click deployment url in the next section. (to allow others to easily deploy your application)
14+
15+
## One click deployment
16+
17+
[<img src="https://www.openmesh.network/xnode/deploy-on-xnode.svg" width=200 />](https://www.openmesh.network/xnode/deploy?advanced=eyJkYXRhIjp7Im5hbWUiOiJ4bm9kZS1ub2RlanMtdGVtcGxhdGUiLCJkZXNjIjoiQ3VzdG9tIEFwcCIsIm5peE5hbWUiOiJ4bm9kZS1ub2RlanMtdGVtcGxhdGUiLCJvcHRpb25zIjpbeyJuYW1lIjoiZW5hYmxlIiwiZGVzYyI6IkVuYWJsZSB0aGUgbmV4dGpzIGFwcCIsIm5peE5hbWUiOiJlbmFibGUiLCJ0eXBlIjoiYm9vbGVhbiIsInZhbHVlIjoidHJ1ZSJ9LHsibmFtZSI6Imhvc3RuYW1lIiwiZGVzYyI6IlRoZSBob3N0bmFtZSB1bmRlciB3aGljaCB0aGUgYXBwIHNob3VsZCBiZSBhY2Nlc3NpYmxlLiIsIm5peE5hbWUiOiJob3N0bmFtZSIsInR5cGUiOiJzdHJpbmciLCJ2YWx1ZSI6IjAuMC4wLjAifSx7Im5hbWUiOiJwb3J0IiwiZGVzYyI6IlRoZSBwb3J0IHVuZGVyIHdoaWNoIHRoZSBhcHAgc2hvdWxkIGJlIGFjY2Vzc2libGUuIiwibml4TmFtZSI6InBvcnQiLCJ0eXBlIjoiMTYgYml0IHVuc2lnbmVkIGludGVnZXI7IGJldHdlZW4gMCBhbmQgNjU1MzUgKGJvdGggaW5jbHVzaXZlKSIsInZhbHVlIjoiMzAwMCJ9LHsibmFtZSI6Im9wZW5GaXJld2FsbCIsImRlc2MiOiJXaGV0aGVyIHRvIG9wZW4gcG9ydHMgaW4gdGhlIGZpcmV3YWxsIGZvciB0aGlzIGFw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joiV2hldGhlciB0byBvcGVuIHBvcnRzIGluIHRoZSBmaXJld2FsbCBmb3IgdGhpcyBhcHBsaWNhdGlvbi4iLCJuaXhOYW1lIjoib3BlbkZpcmV3YWxsIiwidHlwZSI6ImJvb2xlYW4iLCJ2YWx1ZSI6InRydWUifV0sInRhZ3MiOltdLCJmbGFrZXMiOlt7Im5hbWUiOiJ4bm9kZS1ub2RlanMtdGVtcGxhdGUtZmxha2UiLCJ1cmwiOiJnaXRodWI6T3Blbm1lc2gtTmV0d29yay94bm9kZS1ub2RlanMtdGVtcGxhdGUifV19XX0=)
18+
19+
## Commands (in root folder)
20+
21+
```
22+
nix run
23+
```
24+
25+
## Commands (in nodejs-app)
26+
27+
```
28+
npm i
29+
npm run build
30+
npm run start
31+
```

example/flake.nix

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
inputs = {
3+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
4+
xnode-nodejs-template.url = "github:Openmesh-Network/xnode-nodejs-template";
5+
};
6+
7+
outputs =
8+
{
9+
self,
10+
nixpkgs,
11+
xnode-nodejs-template,
12+
...
13+
}:
14+
let
15+
system = "x86_64-linux";
16+
in
17+
{
18+
nixosConfigurations.container = nixpkgs.lib.nixosSystem {
19+
inherit system;
20+
specialArgs = {
21+
inherit xnode-nodejs-template;
22+
};
23+
modules = [
24+
(
25+
{ xnode-nodejs-template, ... }:
26+
{
27+
imports = [
28+
xnode-nodejs-template.nixosModules.default
29+
];
30+
31+
boot.isContainer = true;
32+
33+
services.xnode-nodejs-template = {
34+
enable = true;
35+
};
36+
37+
networking = {
38+
useHostResolvConf = nixpkgs.lib.mkForce false;
39+
};
40+
41+
services.resolved.enable = true;
42+
43+
system.stateVersion = "25.05";
44+
}
45+
)
46+
];
47+
};
48+
};
49+
}

flake.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
description = "Nodejs app running on Xnode!";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
6+
systems.url = "github:nix-systems/default";
7+
};
8+
9+
outputs =
10+
{
11+
self,
12+
nixpkgs,
13+
systems,
14+
}:
15+
let
16+
# A helper that helps us define the attributes below for
17+
# all systems we care about.
18+
eachSystem =
19+
f:
20+
nixpkgs.lib.genAttrs (import systems) (
21+
system:
22+
f {
23+
inherit system;
24+
pkgs = nixpkgs.legacyPackages.${system};
25+
}
26+
);
27+
in
28+
{
29+
packages = eachSystem (
30+
{ pkgs, ... }:
31+
{
32+
default = pkgs.callPackage ./nix/package.nix { };
33+
}
34+
);
35+
36+
checks = eachSystem (
37+
{ pkgs, system, ... }:
38+
{
39+
package = self.packages.${system}.default;
40+
nixos-module = pkgs.callPackage ./nix/nixos-test.nix { };
41+
}
42+
);
43+
44+
nixosModules.default = ./nix/nixos-module.nix;
45+
};
46+
}

nix/nixos-module.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
config,
3+
pkgs,
4+
lib,
5+
...
6+
}:
7+
let
8+
cfg = config.services.xnode-nodejs-template;
9+
xnode-nodejs-template = pkgs.callPackage ./package.nix { };
10+
in
11+
{
12+
options = {
13+
services.xnode-nodejs-template = {
14+
enable = lib.mkEnableOption "Enable the node.js app";
15+
};
16+
};
17+
18+
config = lib.mkIf cfg.enable {
19+
users.groups.xnode-nodejs-template = { };
20+
users.users.xnode-nodejs-template = {
21+
isSystemUser = true;
22+
group = "xnode-nodejs-template";
23+
};
24+
25+
systemd.services.xnode-nodejs-template = {
26+
wantedBy = [ "multi-user.target" ];
27+
description = "Node.js App.";
28+
after = [ "network.target" ];
29+
serviceConfig = {
30+
ExecStart = "${lib.getExe xnode-nodejs-template}";
31+
User = "xnode-nodejs-template";
32+
Group = "xnode-nodejs-template";
33+
};
34+
};
35+
};
36+
}

nix/package.nix

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{ pkgs, lib }:
2+
let
3+
pname = "xnode-nodejs-template";
4+
in
5+
pkgs.buildNpmPackage {
6+
inherit pname;
7+
version = "1.0.0";
8+
src = ../nodejs-app;
9+
10+
npmDeps = pkgs.importNpmLock {
11+
npmRoot = ../nodejs-app;
12+
};
13+
npmConfigHook = pkgs.importNpmLock.npmConfigHook;
14+
15+
postBuild = ''
16+
# Add a shebang to the server js file, then patch the shebang to use a
17+
# nixpkgs nodes binary
18+
sed -i '1s|^|#!/usr/bin/env node\n|' build/index.js
19+
patchShebangs build/index.js
20+
'';
21+
22+
installPhase = ''
23+
mkdir -p $out
24+
cp package.json $out/package.json
25+
cp -r node_modules $out/node_modules
26+
cp -r build $out/build
27+
chmod +x $out/build/index.js
28+
makeWrapper $out/build/index.js $out/bin/xnode-nodejs-template
29+
'';
30+
31+
doDist = false;
32+
33+
meta = {
34+
mainProgram = "xnode-nodejs-template";
35+
};
36+
}

nodejs-app/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.env
3+
build

nodejs-app/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello, world!");

nodejs-app/package-lock.json

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nodejs-app/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "xnode-nodejs-app",
3+
"version": "1.0.0",
4+
"main": "index.ts",
5+
"type": "module",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"build": "rm -rf build && npx tsc",
9+
"start": "node build/index.js"
10+
},
11+
"dependencies": {},
12+
"devDependencies": {
13+
"@types/node": "^20.14.5",
14+
"typescript": "^5.4.5"
15+
}
16+
}

nodejs-app/tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"strict": true,
7+
"strictNullChecks": true,
8+
"esModuleInterop": true,
9+
"outDir": "build",
10+
"skipLibCheck": true
11+
},
12+
"exclude": ["node_modules"]
13+
}

0 commit comments

Comments
 (0)