Skip to content

Commit

Permalink
Sample environments test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Oct 10, 2024
1 parent 15c7d1c commit 42d326b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 73 deletions.
42 changes: 1 addition & 41 deletions modules/base.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,9 @@ with lib;
default = {};
};

builtShells = mkOption {
type = types.attrsOf types.package;
default = {};
};

packages = mkOption {
type = types.listOf types.package;
default = [];
example = literalExpression "[ pkgs.firefox pkgs.thunderbird ]";
description = ''
The set of packages that are symlinked into the environment.
'';
};

channels = mkOption {
type = types.attrs;
type = types.attrsOf types.package;
default = {};
description = ''
Channels, passed through to UI metadata.
'';
};

labeledPackages = mkOption {
type = types.listOf (types.submodule {
options = {
channel = mkOption rec {
type = types.str;
description = "Channel name";
};
attr = mkOption rec {
type = types.str;
description = "Attr name";
};
contents = mkOption rec {
type = types.package;
description = "Package";
};
};
});
default = [];
description = ''
Packages that are labeled with channels and attributes. Used to generate UI metadata.
'';
};
};

Expand Down
4 changes: 3 additions & 1 deletion modules/shells/bash/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ with lib;
};

config = mkIf config.shells.bash.enable {
packages = [ (config.pkgs.callPackage ./. {}) ];
packages = {
"shells.bash" = config.pkgs.callPackage ./. {};
};
};
}
4 changes: 3 additions & 1 deletion modules/shells/fish/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ with lib;
};

config = mkIf config.shells.fish.enable {
packages = [(config.pkgs.callPackage ./. {})];
packages = {
"shells.fish" = config.pkgs.callPackage ./. {};
};
};
}
4 changes: 3 additions & 1 deletion modules/shells/zsh/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ with lib;
};

config = mkIf config.shells.zsh.enable {
packages = [ (config.pkgs.callPackage ./. {}) ];
packages = {
"shells.zsh" = config.pkgs.callPackage ./. {};
};
};
}
24 changes: 4 additions & 20 deletions nix/makeEnvironment.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ let
passthru = old.passthru // {
name = "kernels." + kernel.name;

# channel = kernel.channel;
channel = "codedown";

settings = removeNonDefaultSettings kernel.settingsSchema kernel.settings;
settingsSchema = mapAttrs (_: v: removeAttrs v ["loc"]) kernel.settingsSchema;
};
Expand Down Expand Up @@ -68,35 +65,22 @@ in
symlinkJoin {
inherit name;
paths =
attrValues (evaluated.config.builtKernels)
++ attrValues (evaluated.config.builtLanguageServers)
attrValues evaluated.config.builtKernels
++ attrValues evaluated.config.builtLanguageServers
++ lib.optionals (builtins.length exporters > 0) [(writeTextDir "lib/codedown/exporters.yaml" (lib.generators.toYAML {} exporters))]
++ evaluated.config.packages
++ map (x: x.contents) evaluated.config.labeledPackages
++ attrValues evaluated.config.packages
;

passthru = rec {
inherit evaluated;

inherit channels;

ui_metadata = {
channels = lib.mapAttrsToList (name: channel: channel // {
name = name;
}) evaluated.config.channels;

packages =
(mapAttrs' (n: v: nameValuePair "exporters.${n}" (mkPackageUiMetadata v)) builtExporters)
// (mapAttrs' (n: v: nameValuePair "kernels.${n}" (mkPackageUiMetadata v)) builtKernels)
// (mapAttrs' (n: v: nameValuePair "language-servers.${n}" (mkPackageUiMetadata v)) builtLanguageServers)
// (listToAttrs (map (pkg: nameValuePair pkg.name (mkPackageUiMetadata pkg)) evaluated.config.packages))
// (mapAttrs' (n: v: nameValuePair n (mkPackageUiMetadata v)) evaluated.config.packages)
;

other_packages = map (p: {
channel = p.channel;
attr = p.attr;
meta = if p.contents ? "meta" then uiMetadata.chooseInterestingMeta p.contents else {};
}) evaluated.config.labeledPackages;
};

ui_metadata_yaml = writeText "ui-metadata.yaml" (lib.generators.toYAML {} ui_metadata);
Expand Down
22 changes: 13 additions & 9 deletions tests/app/Spec/Tests/SampleEnvironments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Logger
import Data.Aeson as A
import qualified Data.List as L
import qualified Data.Map as M
import Data.String.Interpolate
import qualified Data.Text as T
import Data.Text as T
import qualified Data.Yaml as Yaml
import System.FilePath
import Test.Sandwich as Sandwich
Expand All @@ -22,7 +24,7 @@ import UnliftIO.Directory
tests :: TopSpec
tests = describe "Sample environments" $ introduceBootstrapNixpkgs $ introduceJustBubblewrap $ do
parallelN 4 $
forM_ fileList $ \file -> do
forM_ (L.sort fileList) $ \file -> do
describe [i|#{file}|] $ do
it "Builds" $ do
let name = T.dropEnd 4 (T.pack file) -- Drop the .nix suffix
Expand All @@ -31,22 +33,24 @@ tests = describe "Sample environments" $ introduceBootstrapNixpkgs $ introduceJu

it "Has well-formed UI metadata" $ do
let name = T.dropEnd 4 (T.pack file) -- Drop the .nix suffix
envRoot <- testBuildUsingFlake [i|.\#sample_environment_#{name}.ui_metadata_yaml|]
info [i|Got metadata YAML: #{envRoot}|]

NixHydrationResult {..} <- liftIO (Yaml.decodeFileEither envRoot) >>= \case
envRoot <- testBuildUsingFlake [i|.\#sample_environment_#{name}|]

yamlPath <- testBuildUsingFlake [i|.\#sample_environment_#{name}.ui_metadata_yaml|]
NixHydrationResult {..} <- liftIO (Yaml.decodeFileEither yamlPath) >>= \case
Left err -> expectationFailure [i|Couldn't decode UI metadata YAML: #{err}|]
Right x -> pure x
info [i|packages: #{A.encode nixHydrationResultPackages}|]

forM_ nixHydrationResultPackages (validatePackage envRoot)
forM_ (M.toList nixHydrationResultPackages) (\(n, v) -> validatePackage envRoot n v)

validatePackage :: (MonadLoggerIO m, MonadFail m) => FilePath -> NixPackage -> m ()
validatePackage envRoot (NixPackage {nixPackageMeta=(NixMeta {..}), ..}) = do
when ("shells." `T.isPrefixOf` nixPackageName) $ do
validatePackage :: (MonadLoggerIO m, MonadFail m) => FilePath -> Text -> NixPackage -> m ()
validatePackage envRoot attr (NixPackage {nixPackageMeta=(NixMeta {..}), ..}) = do
when ("shells." `T.isPrefixOf` attr) $ do
info [i|(#{nixPackageName}) Shell detected; checking it has a mainProgram|]
shouldBeJust nixMetaMainProgram
Just program <- return nixMetaMainProgram
info [i|(#{nixPackageName}) Checking for #{envRoot </> "bin" </> program}|]
doesPathExist (envRoot </> "bin" </> program) >>= (`shouldBe` True)


Expand Down

0 comments on commit 42d326b

Please sign in to comment.