@@ -27,7 +27,7 @@ import Network.Wai
2727import qualified Network.Wai.Handler.Warp as Warp
2828import Network.Wai.Middleware.RequestLogger (logStdout )
2929import NovaCache.NarInfo (NarInfo (.. ), parseNarInfo , renderNarInfo )
30- import NovaCache.Signing (SecretKey , parseSecretKey , sign )
30+ import NovaCache.Signing (SecretKey , normalizeKeyText , parseSecretKey , renderPublicKey , sign , toPublicKey )
3131import NovaCache.Store (CacheInfo (.. ), FileStore , getCacheInfo , listNarInfoHashes , newFileStore , readNar , readNarInfo , writeNar , writeNarInfo )
3232import NovaCache.StorePath (defaultStoreDir , parseStorePath , storePathHashString )
3333import NovaCache.Validate (validateNarInfo )
@@ -78,7 +78,10 @@ maxNarBodySize = 4 * 1024 * 1024 * 1024 -- 4 GB
7878data Config = Config
7979 { cfgStore :: ! FileStore ,
8080 cfgApiKey :: ! (Maybe BS. ByteString ),
81- cfgSigningKey :: ! (Maybe SecretKey )
81+ cfgSigningKey :: ! (Maybe SecretKey ),
82+ -- | The rendered @name:base64@ public key line, derived from the
83+ -- signing key at startup; shown on the landing page.
84+ cfgPublicKey :: ! (Maybe Text )
8285 }
8386
8487-- ---------------------------------------------------------------------------
@@ -107,12 +110,14 @@ main = do
107110
108111 store <- newFileStore storeRoot
109112 sigKey <- loadSigningKey sigKeyPath
113+ pubKey <- derivePublicKeyLine sigKey
110114
111115 let cfg =
112116 Config
113117 { cfgStore = store,
114- cfgApiKey = TE. encodeUtf8 . T. pack <$> apiKeyEnv,
115- cfgSigningKey = sigKey
118+ cfgApiKey = TE. encodeUtf8 . normalizeKeyText . T. pack <$> apiKeyEnv,
119+ cfgSigningKey = sigKey,
120+ cfgPublicKey = pubKey
116121 }
117122
118123 let logRequests = logRequestsEnv /= Just " 0"
@@ -121,6 +126,7 @@ main = do
121126 putStrLn (" nova-cache-server listening on port " ++ show port)
122127 putStrLn (" store root: " ++ storeRoot)
123128 putStrLn (" signing: " ++ maybe " disabled" (const " enabled" ) sigKey)
129+ mapM_ (\ k -> putStrLn (" public key: " ++ T. unpack k)) pubKey
124130 putStrLn (" write auth: " ++ maybe " disabled (open writes!)" (const " enabled" ) (cfgApiKey cfg))
125131 putStrLn (" request logging: " ++ if logRequests then " enabled" else " disabled (LOG_REQUESTS=0)" )
126132
@@ -147,12 +153,23 @@ loadSigningKey :: Maybe FilePath -> IO (Maybe SecretKey)
147153loadSigningKey Nothing = pure Nothing
148154loadSigningKey (Just path) = do
149155 raw <- BS. readFile path
150- case first show (TE. decodeUtf8' raw) >>= parseSecretKey . T. strip of
156+ case first show (TE. decodeUtf8' raw) >>= parseSecretKey . normalizeKeyText of
151157 Left err -> do
152158 hPutStrLn stderr (" WARNING: failed to load signing key: " ++ err)
153159 pure Nothing
154160 Right sk -> pure (Just sk)
155161
162+ -- | Derive the rendered public key line from the signing key, if any.
163+ -- The landing page shows this line, so the published trust anchor always
164+ -- matches the key in use; on a derivation failure the page omits it.
165+ derivePublicKeyLine :: Maybe SecretKey -> IO (Maybe Text )
166+ derivePublicKeyLine Nothing = pure Nothing
167+ derivePublicKeyLine (Just sk) = case toPublicKey sk of
168+ Left err -> do
169+ hPutStrLn stderr (" WARNING: cannot derive the public key from the signing key: " ++ err)
170+ pure Nothing
171+ Right pk -> pure (Just (renderPublicKey pk))
172+
156173-- ---------------------------------------------------------------------------
157174-- WAI application
158175-- ---------------------------------------------------------------------------
@@ -165,7 +182,7 @@ app cfg req respond = case (requestMethod req, pathInfo req) of
165182 pathCount <- length <$> listNarInfoHashes (cfgStore cfg)
166183 let info = getCacheInfo (cfgStore cfg)
167184 signingEnabled = isJust (cfgSigningKey cfg)
168- body = TE. encodeUtf8 (landingHtml info signingEnabled pathCount)
185+ body = TE. encodeUtf8 (landingHtml info signingEnabled (cfgPublicKey cfg) pathCount)
169186 respond (responseLBS HTTP. status200 htmlHeaders (BL. fromStrict body))
170187 -- GET /nix-cache-info
171188 (" GET" , [" nix-cache-info" ]) ->
@@ -370,11 +387,12 @@ boolText :: Bool -> Text
370387boolText True = " 1"
371388boolText False = " 0"
372389
373- -- | The landing page served at @GET /@. Static apart from three live
374- -- values (store-path count, store dir, signing status); styled to match
375- -- novavero.ai. No user input is interpolated, so no escaping is needed.
376- landingHtml :: CacheInfo -> Bool -> Int -> Text
377- landingHtml info signingEnabled pathCount =
390+ -- | The landing page served at @GET /@. Static apart from four live
391+ -- values (store-path count, store dir, signing status, public key); styled
392+ -- to match novavero.ai. Nothing user-supplied is interpolated — the key
393+ -- line is operator configuration — so no escaping is needed.
394+ landingHtml :: CacheInfo -> Bool -> Maybe Text -> Int -> Text
395+ landingHtml info signingEnabled pubKey pathCount =
378396 T. unlines
379397 [ " <!DOCTYPE html>" ,
380398 " <html lang=\" en\" >" ,
@@ -416,7 +434,7 @@ landingHtml info signingEnabled pathCount =
416434 " <div class=\" stat\" ><div class=\" value\" >" <> T. pack (show (ciPriority info)) <> " </div><div class=\" label\" >priority</div></div>" ,
417435 " </div>" ,
418436 " <h2>Use it</h2>" ,
419- " <pre><code>substituters = https://cache.novavero.ai</code></pre>" ,
437+ " <pre><code>substituters = https://cache.novavero.ai" <> trustAnchorLine <> " </code></pre>" ,
420438 " <p>Store dir: <code>" <> ciStoreDir info <> " </code> · protocol endpoints: <code>/nix-cache-info</code>, <code>/<hash>.narinfo</code>, <code>/nar/<file></code></p>" ,
421439 " <h2>What this is</h2>" ,
422440 " <p>The binary cache behind the Novavero Nix toolchain. Powered by <a href=\" https://github.com/Novavero-AI/nova-cache\" >nova-cache</a>, a Haskell implementation of the Nix binary cache protocol. Read about the first package built by Nix natively on Windows on <a href=\" https://novavero.ai/blog/first-native-windows-nix-build.html\" >the blog</a>.</p>" ,
@@ -425,6 +443,8 @@ landingHtml info signingEnabled pathCount =
425443 " </body>" ,
426444 " </html>"
427445 ]
446+ where
447+ trustAnchorLine = maybe " " (" \n trusted-public-keys = " <> ) pubKey
428448
429449-- | The Novavero mark (the novavero.ai favicon), inlined so the page stays
430450-- a single self-contained response with no external assets.
0 commit comments