From 401b0a5c23f899b46b563784ee0843e2a7215d22 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sat, 28 Oct 2023 14:35:42 +0200 Subject: [PATCH] [nix] flake init --- flake.lock | 27 +++++++++++ flake.nix | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..f4524e7 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1697915759, + "narHash": "sha256-WyMj5jGcecD+KC8gEs+wFth1J1wjisZf8kVZH13f1Zo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "51d906d2341c9e866e48c2efcaac0f2d70bfd43e", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3868407 --- /dev/null +++ b/flake.nix @@ -0,0 +1,135 @@ +{ + description = "A very basic flake"; + + #nixpkgs for testing framework + inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + + outputs = { self, nixpkgs, ... }: + let + forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ]; + in + { + packages.x86_64-linux.gokill = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec { + pname = "gokill"; + version = "1.0"; + vendorHash = "sha256-aKEOMeW9QVSLsSuHV4b1khqM0rRrMjJ6Eu5RjY+6V8k="; + src = ./.; + + postInstall = '' + ''; + }; + + packages.x86_64-linux.default = self.packages.x86_64-linux.gokill; + + nixosModules.gokill = { config, lib, pkgs, ... }: + let + cfg = config.services.gokill; + configFile = pkgs.writeText "config.json" ''${cfg.extraConfig}''; + gokill-pkg = self.packages.x86_64-linux.gokill; + in + { + options = { + services.gokill = { + enable = lib.mkOption { + default = false; + type = lib.types.bool; + description = lib.mdDoc '' + Enables gokill daemon + ''; + }; + + extraConfig = lib.mkOption { + type = lib.types.str; + description = lib.mdDoc '' + gokill config.json + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.gokill = { + description = "gokill daemon"; + serviceConfig = { + Type = "simple"; + ExecStart = "${gokill-pkg}/bin/gokill -c ${configFile}"; + Restart = "on-failure"; + }; + + wantedBy = [ "default.target" ]; + }; + }; + }; + + packages.x86_64-linux.testVm = + let + nixos = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + self.nixosModules.gokill + { + services.gokill.enable = true; + services.gokill.extraConfig = '' + [ + { + "type": "Timeout", + "name": "custom timeout", + "options": { + "duration": 30 + }, + "actions": [ + { + "type": "Print", + "options": { + "message": "Stage 1 triggered. Waiting 25 seconds" + }, + "stage": 1 + }, + { + "type": "Timeout", + "options": { + "duration": 20 + }, + "stage": 1 + }, + { + "type": "Timeout", + "options": { + "duration": 5 + }, + "stage": 2 + }, + { + "type": "Print", + "options": { + "message": "Shutdown in 5 seconds..." + }, + "stage": 2 + }, + { + "type": "Shutdown", + "options": { + }, + "stage": 3 + } + ] + } + ] + ''; + users.users.root.password = "root"; + virtualisation.vmVariant.virtualisation.graphics = false; + } + ]; + + }; + in + nixos.config.system.build.vm; + + apps.x86_64-linux.testVm = { + type = "app"; + program = builtins.toString (nixpkgs.legacyPackages."x86_64-linux".writeScript "vm" '' + ${self.packages."x86_64-linux".testVm}/bin/run-nixos-vm + ''); + }; + }; +}