diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..05a5442f --- /dev/null +++ b/flake.lock @@ -0,0 +1,64 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1706830856, + "narHash": "sha256-a0NYyp+h9hlb7ddVz4LUn1vT/PLwqfrWYcHMvFB1xYg=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "b253292d9c0a5ead9bc98c4e9a26c6312e27d69f", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1709161043, + "narHash": "sha256-8rY3xrbE2J7nT1r2n1mELIXVqbeHF/osZ9ttjqgoP14=", + "owner": "hercules-ci", + "repo": "nixpkgs", + "rev": "46096c0a6d406ae82efda8ac5dbeccafcb575d5c", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "ref": "update-nixops_unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1706550542, + "narHash": "sha256-UcsnCG6wx++23yeER4Hg18CXWbgNpqNXcHIo5/1Y+hc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "97b17f32362e475016f942bbdfda4a4a72a8a652", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..954730ef --- /dev/null +++ b/flake.nix @@ -0,0 +1,31 @@ +{ + description = "Description for the project"; + + inputs = { + flake-parts.url = "github:hercules-ci/flake-parts"; + nixpkgs.url = "github:hercules-ci/nixpkgs/update-nixops_unstable"; + }; + + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ + ]; + debug = true; + systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ]; + perSystem = { config, self', inputs', pkgs, system, ... }: { + packages.nixops_with_aws = + (pkgs.nixops_unstable_minimal.addAvailablePlugins (self: super: { + nixops-aws = super.callPackage ./nix/nixops-aws.nix { }; + })).withPlugins (plugins: [ plugins.nixops-aws ]); + packages.plugin = config.packages.nixops_with_aws.availablePlugins.nixops-aws; + # `plugin` would have been a valid choice, but a nixops is more useful. It builds fast. + packages.default = config.packages.nixops_with_aws; + devShells.default = + pkgs.mkShell { + inputsFrom = [ config.packages.plugin ]; + }; + }; + flake = { + }; + }; +} diff --git a/nix/nixops-aws.nix b/nix/nixops-aws.nix new file mode 100644 index 00000000..bad733ef --- /dev/null +++ b/nix/nixops-aws.nix @@ -0,0 +1,62 @@ +{ lib +, buildPythonPackage +, fetchFromGitHub +, unstableGitUpdater +, poetry-core +, boto +, boto3 +, nixops +, nixos-modules-contrib +, typing-extensions +}: + +buildPythonPackage { + pname = "nixops-aws"; + version = "unstable-2023-08-09"; + pyproject = true; + + src = + with lib.fileset; + toSource { + root = ../.; + fileset = unions [ + ../nixops_aws + ../pyproject.toml + ../setup.cfg + ../tests + ../tests.py + ]; + }; + + postPatch = '' + substituteInPlace pyproject.toml \ + --replace poetry.masonry.api poetry.core.masonry.api \ + --replace "poetry>=" "poetry-core>=" + ''; + + nativeBuildInputs = [ + poetry-core + ]; + + buildInputs = [ + nixops + ]; + + propagatedBuildInputs = [ + boto + boto3 + nixos-modules-contrib + typing-extensions + ]; + + pythonImportsCheck = [ "nixops_aws" ]; + + passthru.updateScript = unstableGitUpdater {}; + + meta = with lib; { + description = "AWS plugin for NixOps"; + homepage = "https://github.com/NixOS/nixops-aws"; + license = licenses.lgpl3Only; + maintainers = nixops.meta.maintainers; + }; +} diff --git a/nixops_aws/nix/ec2.nix b/nixops_aws/nix/ec2.nix index f1c8aeb9..0ca767b8 100644 --- a/nixops_aws/nix/ec2.nix +++ b/nixops_aws/nix/ec2.nix @@ -1,12 +1,13 @@ # Configuration specific to the EC2 backend. -{ config, pkgs, lib, utils, ... }: +{ config, pkgs, lib, options, utils, ... }: with utils; with lib; with import ./lib.nix lib; let + throwIf = lib.throwIf or (cond: msg: if cond then throw msg else x: x); types = if lib.types ? either then @@ -165,7 +166,21 @@ let nixosVersion = builtins.substring 0 5 (config.system.nixos.version or config.system.nixosVersion); - amis = import ; + amisLegacy = import (pkgs.path + "/nixos/modules/virtualisation/ec2-amis.nix"); + + amisPath = pkgs.path + "/nixos/modules/virtualisation/amazon-ec2-amis.nix"; + amis = import amisPath; + + lookupAMI = { nixosVersion, region, system, virtType }: + assert builtins.trace "Looking up AMI for ${nixosVersion} in ${region} for ${system} with ${virtType}..." true; + if builtins.pathExists amisPath + then + (((amis.${nixosVersion} or amis.latest) + .${region} or (throw "No AMIs for region ${region}")) + .${system} or (throw "No AMIs for instance type ${system} in region ${region}")) + .${virtType} or (throw "No AMI for virtualisation type ${virtType} on instance type ${system} in region ${region}") + else + (amisLegacy.${nixosVersion} or amisLegacy.latest).${region}.${virtType}; in @@ -484,7 +499,7 @@ in config = mkIf (config.deployment.targetEnv == "ec2") { - nixpkgs.hostPlatform = + nixpkgs.${if options?nixpkgs.hostPlatform then "hostPlatform" else "system"} = let checked = throwIf (config.deployment.ec2.physicalProperties.platforms or [] == []) @@ -507,7 +522,7 @@ in < ranking.${b.cpu} or unrankedRank; systemString = - lib.throwIf (plat.os or "linux" != "linux") + throwIf (plat.os or "linux" != "linux") "Instance does not seem to be intended for running Linux. Please set nixpkgs.hostPlatform manually." plat.cpu + "-linux"; in @@ -516,20 +531,18 @@ in deployment.ec2.ami = mkDefault ( let # FIXME: select hvm-s3 AMIs if appropriate. - type = + virtType = if isEc2Hvm then if cfg.ebsBoot then "hvm-ebs" else "hvm-s3" else if cfg.ebsBoot then "pv-ebs" else "pv-s3"; - amis' = amis."${nixosVersion}" or amis.latest; in - with builtins; - if hasAttr cfg.region amis' then - let r = amis'."${cfg.region}"; - in if hasAttr type r then r."${type}" else - throw "I don't know an AMI for virtualisation type ${type} with instance type ${cfg.instanceType}" - else - throw "I don't know an AMI for region ‘${cfg.region}’ and platform type ‘${config.nixpkgs.hostPlatform.system}’" + lookupAMI { + inherit virtType; + inherit nixosVersion; + inherit (cfg) region; + system = config.nixpkgs.hostPlatform.system or config.nixpkgs.system; + } ); # Workaround: the evaluation of blockDeviceMapping requires fileSystems to be defined. diff --git a/pyproject.toml b/pyproject.toml index 10e1ae24..02b2d567 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ python = "^3.7" boto = "^2.49.0" boto3 = "^1.13.7" nixops = {git = "https://github.com/NixOS/nixops.git", rev = "master"} -typing-extensions = "^3.7.4" +typing-extensions = ">=3.7 <=5" nixos-modules-contrib = {git = "https://github.com/nix-community/nixos-modules-contrib.git", rev = "master"} [tool.poetry.dev-dependencies]