working container

This commit is contained in:
Anish Lakhwara 2022-04-16 18:13:02 +10:00
parent c950ce40c9
commit 613d4447bf

214
flake.nix
View file

@ -8,113 +8,117 @@
}; };
outputs = { self, nixpkgs, flake-utils, poonam }: outputs = { self, nixpkgs, flake-utils, poonam }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachDefaultSystem
let (system:
pkgs = nixpkgs.legacyPackages.${system}; let
my-python = pkgs.python3; pkgs = nixpkgs.legacyPackages.${system};
python-with-my-packages = my-python.withPackages (p: with p; [ my-python = pkgs.python3;
jinja2 python-with-my-packages = my-python.withPackages (p: with p; [
click jinja2
# other python packages you want in the devShell click
]); # other python packages you want in the devShell
python-deps = with pkgs.python3Packages; [ ]);
# Packages necessary at runtime to run build script python-deps = with pkgs.python3Packages; [
jinja2 # Packages necessary at runtime to run build script
python-ctags3 jinja2
]; python-ctags3
in
{
devShell = pkgs.mkShell {
buildInputs = [
python-with-my-packages
pkgs.pandoc
]; ];
shellHook = '' in
PYTHONPATH=${python-with-my-packages}/${python-with-my-packages.sitePackages} {
# This only works when you have ~/vimwiki locally, since I'm not putting my unfiltered kitaab online devShell = pkgs.mkShell {
alias build='python3 src/kitaab-public/main.py' buildInputs = [
# maybe set more env-vars python-with-my-packages
''; pkgs.pandoc
}; ];
packages.basant = pkgs.python3Packages.buildPythonPackage { shellHook = ''
pname = "basant"; PYTHONPATH=${python-with-my-packages}/${python-with-my-packages.sitePackages}
src = ./.; # This only works when you have ~/vimwiki locally, since I'm not putting my unfiltered kitaab online
version = "0.1"; alias build='python3 src/kitaab-public/main.py'
propagatedBuildInputs = [ python-deps ]; # maybe set more env-vars
doCheck = false; '';
}; };
packages.site = pkgs.stdenv.mkDerivation { packages.basant = pkgs.python3Packages.buildPythonPackage {
name = "anish-lakhwara-site"; pname = "basant";
src = ./.; src = ./.;
buildInputs = [ poonam.defaultPackage.${system} self.packages.${system}.basant ]; version = "0.1";
configurePhase = '' propagatedBuildInputs = [ python-deps ];
doCheck = false;
};
packages.site = pkgs.stdenv.mkDerivation {
name = "anish-lakhwara-site";
src = ./.;
buildInputs = [ poonam.defaultPackage.${system} self.packages.${system}.basant ];
configurePhase = ''
cp -r ${poonam.defaultPackage.${system}}/public/ ./ cp -r ${poonam.defaultPackage.${system}}/public/ ./
''; '';
buildPhase = '' buildPhase = ''
${self.packages.${system}.basant}/bin/main.py ${self.packages.${system}.basant}/bin/main.py
''; '';
installPhase = '' installPhase = ''
mkdir -p $out mkdir -p $out
cp -R output/* $out/ cp -R output/* $out/
''; '';
};
defaultPackage = self.packages.${system}.site;
# Test container
nixosConfigurations.container = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModule
({ pkgs, ... }: {
# Only allow this to boot as a container
boot.isContainer = true;
networking.hostName = "basant";
services."anish.lakhwara.com".enable = true;
})
];
};
# also need a module that refreshes this flake.lock, but maybe that shouldn't live here?
nixosModule = { options, lib, config, pkgs, ... }:
let
serverName = "anish.lakhwara.com";
webRoot = "/var/www/${serverName}";
serviceConfig = config.services."${serverName}";
options = {
enable = lib.mkEnableOption "${serverName} service";
};
in
{
options.services.${serverName} = options;
config = lib.mkIf serviceConfig.enable {
systemd.services."source-${serverName}" = {
description = ''
https://${serverName} source
'';
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
DynamicUser = "yes";
};
startAt = "*00:00:00";
path = with pkgs; [ nix gnutar gzip curl jq ] ++ [ self.packages.${system}.site ];
script = ''
set -ex
ln -sfT ${self.packages.${system}.site} ${webRoot}
'';
};
# Allow nginx through the firewall
networking.firewall.allowedTCPPorts = [ 80 ];
services.nginx.enable = true;
services.nginx.virtualHosts.${serverName} = {
forceSSL = true;
enableACME = true;
locations."/" = { root = "${webRoot}"; };
};
};
}; };
}); defaultPackage = self.packages.${system}.site;
# also need a module that refreshes this flake.lock, but maybe that shouldn't live here?
nixosModule = { options, lib, config, pkgs, ... }:
let
serverName = "anish.lakhwara.com";
webRoot = "/var/www/${serverName}";
serviceConfig = config.services."${serverName}";
options = {
enable = lib.mkEnableOption "${serverName} service";
};
in
{
options.services.${serverName} = options;
config = lib.mkIf serviceConfig.enable {
systemd.services."source-${serverName}" = {
description = ''
https://${serverName} source
'';
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
DynamicUser = "yes";
};
startAt = "*00:00:00";
path = with pkgs; [ nix gnutar gzip curl jq ] ++ [ self.packages.${system}.site ];
script = ''
set -ex
ln -sfT ${self.packages.${system}.site} ${webRoot}
'';
};
# Allow nginx through the firewall
networking.firewall.allowedTCPPorts = [ 80 ];
services.nginx.enable = true;
services.nginx.virtualHosts.${serverName} = {
forceSSL = true;
enableACME = true;
locations."/" = { root = "${webRoot}"; };
};
};
};
}) // {
# Test container
nixosConfigurations."test" = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModule
({ pkgs, ... }: {
# Only allow this to boot as a container
boot.isContainer = true;
networking.hostName = "basant";
# Allow nginx through the firewall
networking.firewall.allowedTCPPorts = [ 80 ];
services.nginx.enable = true;
services."anish.lakhwara.com".enable = true;
})
];
};
};
} }