Skip to content

Commit

Permalink
Simplify rust packages schema
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Sep 18, 2024
1 parent 4d1c85c commit 5b76e36
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 15 deletions.
5 changes: 4 additions & 1 deletion modules/base/convert-type.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ let
}
else if (type.name == "submodule") then {
tag = "submodule";
foo = "BAR";
keys = lib.mapAttrsRecursiveCond
(x: !(x ? _type))
(path: value: convertType target value.type)
(lib.removeAttrs (type.getSubOptions {}) ["_module"]);
}
else builtins.throw "Can't convert type for '${target}': ${toString type.name}"
;
Expand Down
6 changes: 3 additions & 3 deletions modules/languages/rust/evcxr/python/build_init_evcxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
with open(out, "a") as f:
for package in packages:
package_name = package if isinstance(package, str) else package["name"]
settings = {} if isinstance(package, str) else package.get("settings", {})
features = {} if isinstance(package, str) else package.get("features", {})
if package_name in name_to_dir:
clauses = [f"""version = \"{name_to_version.get(package_name, "*")}\""""]
if "features" in settings:
clauses.append(f"""features = {json.dumps(settings["features"])}""")
if features:
clauses.append(f"""features = {json.dumps(features)}""")

clauses_joined = ", ".join(clauses)

Expand Down
4 changes: 2 additions & 2 deletions modules/languages/rust/evcxr/withPackages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ let

renderPackage = pn:
if builtins.isString pn then ''${pn} = "*"''
else if builtins.isAttrs pn && lib.hasAttrByPath ["settings" "features"] pn
then ''${pn.name} = { version = "*", features = [${lib.concatStringsSep ", " (map (feat: ''"'' + feat + ''"'') pn.settings.features)}] }''
else if builtins.isAttrs pn && lib.hasAttrByPath ["features"] pn
then ''${pn.name} = { version = "*", features = [${lib.concatStringsSep ", " (map (feat: ''"'' + feat + ''"'') pn.features)}] }''
else ''${pn.name} = { version = "*" }'';

cargoToml = packages: writeTextFile {
Expand Down
2 changes: 1 addition & 1 deletion modules/languages/rust/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ with lib;
type = types.str;
description = "Package name";
};
settings.features = mkOption rec {
features = mkOption rec {
type = types.listOf types.str;
description = "Features to enable for the package";
};
Expand Down
4 changes: 1 addition & 3 deletions sample_environments/rust.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ codedown.makeEnvironment {
"rand"
{
name = "serde";
settings = {
features = ["derive"];
};
features = ["derive"];
}
"serde_json"
];
Expand Down
3 changes: 2 additions & 1 deletion tests/app/Spec/Tests/Rust.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import TestLib.NixEnvironmentContext
import TestLib.NixTypes
import TestLib.TestSearchers
import TestLib.Types
import TestLib.Util


tests :: LanguageSpec
Expand Down Expand Up @@ -68,7 +69,7 @@ kernelSpec = NixKernelSpec {
, nixKernelPackages = [
nameOnly "rand"

, NameAndSettings "serde" (Just (A.object [("features", A.Array (V.fromList ["derive"]))]))
, NameAndSettings "serde" (Just (aesonFromList [("features", A.Array (V.fromList ["derive"]))]))
, nameOnly "serde_json"
, nameOnly "serde_derive"
]
Expand Down
6 changes: 3 additions & 3 deletions tests/src/TestLib/NixRendering.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import qualified Data.Vector as V
import TestLib.NixTypes

#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.KeyMap as HM
import qualified Data.Aeson.KeyMap as HM
#else
import qualified Data.HashMap.Strict as HM
import qualified Data.HashMap.Strict as HM
#endif


Expand Down Expand Up @@ -87,7 +87,7 @@ renderKernel (NixKernelSpec {..}) =
xs -> [i|kernels.#{nixKernelName}.packages = [#{T.unwords $ fmap renderKernelPackage xs}];|]

renderKernelPackage (NameAndSettings name Nothing) = quote name
renderKernelPackage (NameAndSettings name (Just settings)) = aesonToNix (A.object [("name", A.String name), ("settings", settings)])
renderKernelPackage (NameAndSettings name (Just settings)) = aesonToNix (A.object (("name", A.String name) : HM.toList settings))
& parenQuote

parenQuote x = "(" <> x <> ")"
Expand Down
2 changes: 1 addition & 1 deletion tests/src/TestLib/NixTypes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ deriveJSON toSnakeBoth2 ''NixSrcSpec

data NameAndSettings = NameAndSettings {
nameAndSettingsName :: Text
, nameAndSettingsSettings :: Maybe Value
, nameAndSettingsSettings :: Maybe A.Object
} deriving (Show, Eq, Ord)
deriveJSON toSnake3 ''NameAndSettings

Expand Down
8 changes: 8 additions & 0 deletions tests/src/TestLib/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ aesonFromList xs = HM.fromList [(A.fromText k, v) | (k, v) <- xs]
aesonFromList :: (Eq k, Hashable k) => [(Text, Value)] -> HM.HashMap A.Key v
aesonFromList = HM.fromList
#endif

#if MIN_VERSION_aeson(2,0,0)
aesonToList :: HM.KeyMap Value -> [(Text, Value)]
aesonToList m = [(A.toText k, v) | (k, v) <- HM.toList m]
#else
aesonToList :: (Eq k, Hashable k) => HM.HashMap A.Key v -> [(Text, Value)]
aesonToList = HM.toList
#endif

0 comments on commit 5b76e36

Please sign in to comment.