commit 643f58adcf1dad1de6d43b61b3dd749400ce954c Author: Borodinov Ilya Date: Mon Oct 21 23:09:12 2024 +0300 init diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4bc20c1 --- /dev/null +++ b/flake.nix @@ -0,0 +1,13 @@ +{ + description = "ГОЙДА_OS - переделка NixOS на runit"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs }: { + lib = { + makeSystem = import ./make-system.nix { inherit nixpkgs; }; + }; + }; +} diff --git a/make-system.nix b/make-system.nix new file mode 100644 index 0000000..7d0ed33 --- /dev/null +++ b/make-system.nix @@ -0,0 +1,66 @@ +{ nixpkgs }: +{ + modules, + system ? builtins.currentSystem, + platform ? null, + crossSystem ? null, +}: + +let + pkgs = import nixpkgs { + inherit system; + platform = platform; + config = { }; + }; + pkgsModule = + { config, ... }: + { + _file = ./default.nix; + key = ./default.nix; + config = { + nixpkgs.pkgs = ( + import nixpkgs { + inherit system crossSystem; + config = config.nixpkgs.config; + overlays = config.nixpkgs.overlays; + } + ); + nixpkgs.localSystem = + { + inherit system; + } + // pkgs.lib.optionalAttrs (crossSystem != null) { + inherit crossSystem; + }; + }; + }; + baseModules = [ + ./base.nix + ./system-path.nix + ./stage-1.nix + ./stage-2.nix + ./runit.nix + (nixpkgs + "/nixos/modules/system/etc/etc.nix") + (nixpkgs + "/nixos/modules/system/activation/activation-script.nix") + (nixpkgs + "/nixos/modules/misc/nixpkgs.nix") + (nixpkgs + "/nixos/modules/system/boot/kernel.nix") + (nixpkgs + "/nixos/modules/misc/assertions.nix") + (nixpkgs + "/nixos/modules/misc/lib.nix") + (nixpkgs + "/nixos/modules/config/sysctl.nix") + pkgsModule + ]; + other = { + _module.check = true; + _module.args = { }; + }; +in +pkgs.lib.evalModules { + prefix = [ ]; + modules = + modules + ++ baseModules + ++ [ + pkgsModule + other + ]; +} diff --git a/runit.nix b/runit.nix new file mode 100644 index 0000000..a298ea8 --- /dev/null +++ b/runit.nix @@ -0,0 +1,62 @@ +{ + pkgs, + lib, + config, + ... +}: + +let + compat = pkgs.runCommand "runit-compat" { } '' + mkdir -p $out/bin/ + cat << EOF > $out/bin/poweroff +#!/bin/sh +exec runit-init 0 +EOF + cat << EOF > $out/bin/reboot +#!/bin/sh +exec runit-init 6 +EOF + chmod +x $out/bin/{poweroff,reboot} + ''; +in +{ + environment.systemPackages = [ compat ]; + environment.etc = lib.mkMerge [ + { + "runit/1".source = pkgs.writeScript "1" '' + #!${pkgs.runtimeShell} + mkdir /bin/ + ln -s ${pkgs.runtimeShell} /bin/sh + + ${lib.optionalString (config.networking.timeServers != [ ]) '' + ${pkgs.ntp}/bin/ntpdate ${toString config.networking.timeServers} + ''} + + # disable DPMS on tty's + echo -ne "\033[9;0]" > /dev/tty0 + + touch /etc/runit/stopit + chmod 0 /etc/runit/stopit + ''; + "runit/2".source = pkgs.writeScript "2" '' + #!${pkgs.runtimeShell} + exec runsvdir -P /etc/service + ''; + "runit/3".source = pkgs.writeScript "3" '' + #!${pkgs.runtimeShell} + echo and down we go + ''; + "service/nix-daemon/run".source = pkgs.writeScript "nix-daemon" '' + #!${pkgs.runtimeShell} + nix-daemon + ''; + } + (lib.mkIf config.not-os.rngd { + "service/rngd/run".source = pkgs.writeScript "rngd" '' + #!${pkgs.runtimeShell} + export PATH=$PATH:${pkgs.rng-tools}/bin + exec rngd -r /dev/hwrng + ''; + }) + ]; +} diff --git a/services/default.nix b/services/default.nix new file mode 100644 index 0000000..451de3a --- /dev/null +++ b/services/default.nix @@ -0,0 +1,49 @@ +{ lib, config, ... }: +let + cfg = config.runit; +in +{ + options = { + runit.services = lib.mkOption { + type = lib.types.attrsOf ( + lib.types.submodule { + run = lib.mkOption { + type = lib.types.string; + description = "The command to run. Equivalent to ExecStart in systemd"; + }; + check = lib.mkOption { + type = lib.types.optional lib.types.string; + description = "The command to run to check if the service is running."; + }; + finish = lib.mkOption { + type = lib.types.optional lib.types.string; + description = "The command to run when the service is stopped."; + }; + conf = lib.mkOption { + type = lib.types.optional lib.types.string; + description = "The command to run to configure the service."; + }; + } + ); + default = { }; + }; + }; + config = { + environment.etc = lib.mkMerge lib.mapAttrsToList ( + name: value: + { + "service/${name}/run".source = value.run; + + } + // lib.optionalAttrs (value.check != null) { + "service/${name}/check".source = value.check; + } + // lib.optionalAttrs (value.finish != null) { + "service/${name}/finish".source = value.finish; + } + // lib.optionalAttrs (value.conf != null) { + "service/${name}/conf".source = value.conf; + } + ) cfg.services; + }; +}